View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Unique elements in cell array

4.7 | 6 ratings Rate this file 39 Downloads (last 30 days) File Size: 1.4 KB File ID: #31718 Version: 1.0

Unique elements in cell array



unique for numeric cell arrays. If c = {[0],[1,2],[0],[8],[1,2]} returns {[0],[1,2],[8]}

| Watch this File

File Information

function [Au, idx, idx2] = uniquecell(A)

Same as built-in unique, but works for cell arrays where each element is a numeric array.

For A a cell array of matrices (or vectors), returns Au, which contains the unique matrices in A, idx, which contains the indices of the last appearance of each such unique matrix, and idx2, which contains th indices such that Au(idx2) == A
Example usage:
A = {[1,2,3],[0],[2,3,4],[2,3,1],[1,2,3],[0]};
[Au,idx,idx2] = uniquecell(A);
Results in:

idx = [6,5,4,3]
Au = {[0],[1,2,3],[2,3,1],[2,3,4]}
idx2 = [2,1,4,3,2,1]
Algorithm: uses cellfun to translate numeric matrices into strings then calls unique on cell array of strings and reconstructs the initial matrices.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
02 Feb 2017 Abdul-Lateef Opebiyi

Thanks, this function makes my calculation a lot simple

09 Dec 2016 Tobi Busari

This is really helpful!

Comment only
15 Nov 2016 Diego Lozano-Soldevilla

04 Aug 2016 Showmin

Great work.

13 May 2014 I Chieh Chen

I Chieh Chen (view profile)

Really nice, thanks a lot~

05 Nov 2013 Kevin Bartlett

Kevin Bartlett (view profile)

14 Sep 2012 Andrey

Andrey (view profile)

Good, but limited usage only to numeric matrixes.

15 Feb 2012 Jessica Lam

Really Useful.
Thanks so much=)

Comment only

Contact us