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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to find unique pages in a 3d matrix?

Asked by may on 12 Feb 2013

If I have 3d matrix like

    A = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2],[1 2; 3 4]) 

I want to find unique pages in this matrix so the result should be

   result = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2])

0 Comments

may

Products

2 Answers

Answer by Azzi Abdelmalek on 12 Feb 2013
Edited by Azzi Abdelmalek on 12 Feb 2013
Accepted answer
A = cat(3, [1 2; 3 4;0 0], [5 6; 3 4; 0 0], [5 6; 1 2;0 0],[1 2; 3 4;0 0])
[n,m,p]=size(A)
a=reshape(A,n,[],1)
b=reshape(a(:),n*m,[])'
c=unique(b,'rows','stable')'
reshape(c,n,m,[])

0 Comments

Azzi Abdelmalek
Answer by Honglei Chen on 12 Feb 2013

You can try to reshape it to 2D first, then remove duplicates. For example

A = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2],[1 2; 3 4])
Ar = reshape(A,[4 4])
Ar = unique(Ar.','rows','stable').'
reshape(Ar,2,2,[])

I don't quite understand your second question. I think MATLAB automatically removes empty pages. What do you mean by "empty pages"?

3 Comments

may on 12 Feb 2013

you are right I edited the question. thank you

may on 12 Feb 2013

when I use unique(Ar,'rows','stable')

I get this error! ??? Error using ==> unique at 34 Unrecognized option.

Honglei Chen on 12 Feb 2013

Your version does not support 'stable' option, try the following

A = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2],[1 2; 3 4])
Ar = reshape(A,[4 4])
[dummy,idx] = unique(Ar.','rows')
reshape(Ar(:,sort(idx)),2,2,[])
Honglei Chen

Contact us