Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the unique slices of a matrix?

In matlab, if you have a matrix A you can find the matrix B containing all of the unique rows of A as follows:

B = unique(A,'rows');

What I have is a 3d matrix, with rows and columns as the first two dimensions, and one additional dimension ('slices').

How can I get the 3d matrix containing all the unique slices in a matrix A? Here's an example of the kind of functionality I want:

>> A % print out A
A(:,:,1) =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


A(:,:,2) =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


A(:,:,3) =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


A(:,:,4) =

     0     0     0     1
     0     0     1     0
     0     1     0     0
     1     0     0     0

>> unique(A,'slices'); % get unique slices

A(:,:,1) =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


A(:,:,2) =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


A(:,:,3) =

     0     0     0     1
     0     0     1     0
     0     1     0     0
     1     0     0     0
like image 965
Cam Avatar asked Mar 14 '13 23:03

Cam


2 Answers

I would begin by reshaping A so each slice becomes a row (with the reshape command). Then use unique(A, 'rows'). Finally, reshape the unique rows back to the same shape the slices.

For example:

% transforming so each row is a slice in row form
reshaped_A = reshape(A, [], size(A, 3))';

% getting unique rows
unique_rows = unique(reshaped_A, 'rows');

% reshaping back
unique_slices = reshape(unique_rows', size(A, 1), size(A, 2), []);

Or all in one line:

reshape(unique(reshape(A, [], size(A, 3))', 'rows')', size(A, 1), size(A, 2), [])

I haven't checked this above code so use with caution! But it should give the idea.


EDIT

Here it is working on your data (also fixed little bug in above code):

>> reshaped_A = reshape(A, [], size(A, 3))'

reshaped_A =

Columns 1 through 11

16     5     9     4     2    11     7    14     3    10     6
 1     0     0     0     0     1     0     0     0     0     1
16     5     9     4     2    11     7    14     3    10     6
 0     0     0     1     0     0     1     0     0     1     0

Columns 12 through 16

15    13     8    12     1
 0     0     0     0     1
15    13     8    12     1
 0     1     0     0     0

Each of these ^^ rows is one of the original slices

>> unique_rows = unique(reshaped_A, 'rows')

unique_rows =

Columns 1 through 11

 0     0     0     1     0     0     1     0     0     1     0
 1     0     0     0     0     1     0     0     0     0     1
16     5     9     4     2    11     7    14     3    10     6

Columns 12 through 16

 0     1     0     0     0
 0     0     0     0     1
15    13     8    12     1

These ^^ are the unique slices, but in the wrong shape.

>> unique_slices = reshape(unique_rows', size(A, 1), size(A, 2), [])

unique_slices(:,:,1) =

 0     0     0     1
 0     0     1     0
 0     1     0     0
 1     0     0     0


unique_slices(:,:,2) =

 1     0     0     0
 0     1     0     0
 0     0     1     0
 0     0     0     1


unique_slices(:,:,3) =

16     2     3    13
 5    11    10     8
 9     7     6    12
 4    14    15     1
like image 160
Bill Cheatham Avatar answered Oct 13 '22 03:10

Bill Cheatham


A very simple and scalable solution would be:

A = cat(3, [16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1], [1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1], [16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1], [0 0 0 1;0 0 1 0;0 1 0 0;1 0 0 0])
[n,m,p] = size(A);
a = reshape(A,n,[],1);
b = reshape(a(:),n*m,[])';
c = unique(b,'rows', 'stable')';    %If the 'stable' option is supported by your version.
%If the 'stable' option is not supported, but it's still required, use the index vector option, as required. 
%i.e.,
%[c,I,J] = unique(b,'rows');
unique_A = reshape(c,n,m,[])

Results:

A(:,:,1) =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


A(:,:,2) =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


A(:,:,3) =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


A(:,:,4) =

     0     0     0     1
     0     0     1     0
     0     1     0     0
     1     0     0     0


unique_A(:,:,1) =

     0     0     0     1
     0     0     1     0
     0     1     0     0
     1     0     0     0


unique_A(:,:,2) =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


unique_A(:,:,3) =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

Source: How to find unique pages in a 3d matrix?

like image 34
Roney Michael Avatar answered Oct 13 '22 05:10

Roney Michael