Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding number of unique cells

Subject: Finding number of unique cells

From: Ryan Miller

Date: 21 Dec, 2010 16:14:06

Message: 1 of 4

I have a 1xN cell, 'stim', with each cell containing a single matrix. I'd like to find the number of unique matrices contained within stim. I was hoping to be able to use unique(stim), but that doesn't work. I could also use a nested for loop through length of stim and use something like isequal(stim{i},stim{i+j}), but that seems rather ugly. There must be a one-line method of achieving some integer output of the number of unique cells. Any help is much appreciated?

Needs to be R2008b and R2009a compatible.

Subject: Finding number of unique cells

From: Sean de

Date: 21 Dec, 2010 16:33:05

Message: 2 of 4

"Ryan Miller" wrote in message <ieqjoe$35i$1@fred.mathworks.com>...
> I have a 1xN cell, 'stim', with each cell containing a single matrix. I'd like to find the number of unique matrices contained within stim. I was hoping to be able to use unique(stim), but that doesn't work. I could also use a nested for loop through length of stim and use something like isequal(stim{i},stim{i+j}), but that seems rather ugly. There must be a one-line method of achieving some integer output of the number of unique cells. Any help is much appreciated?
>
> Needs to be R2008b and R2009a compatible.

Are the cells all the same size? If they are:
%Data:
C = cell(4,1);
C{1} = magic(3);
C{2} = rand(3);
C{3} = magic(3);
C{4} = ones(3);

%Engine:
nunique = size(unique(cell2mat(reshape(cellfun(@(x)x(:).',C,'uni',false),numel(C),1)),'rows'),1)

If not: group them into same sized pieces first, then run the above on it.

Subject: Finding number of unique cells

From: Sean de

Date: 21 Dec, 2010 16:46:05

Message: 3 of 4

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> %Engine:
> nunique = size(unique(cell2mat(reshape(cellfun(@(x)x(:).',C,'uni',false),numel(C),1)),'rows'),1)

Nota Bene:
The cells have to be the same SIZE for this to work, not the same NUMEL.

E.g.
x1 = [1 3; 2 4];
x2 = [1 2 3 4];

isequal(x1,x2) %Actually equal
isequal(x1(:).',x2(:).') %The core part of the engine above

Subject: Finding number of unique cells

From: Ryan Miller

Date: 21 Dec, 2010 21:03:06

Message: 4 of 4

Thanks Sean de, works perfectly.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us