How to get unique slices of a matrix?

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

 B = unique(A,'rows'); 

I have a three-dimensional matrix with rows and columns as the first two dimensions and another dimension (β€œslices”).

How can I get a 3D matrix containing all the unique slices in matrix A ? Here is an example of what 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 
+4
source share
2 answers

I would start by changing A so that each slice becomes a string (using the reshape ). Then use unique(A, 'rows') . Finally, change the unique lines to the same slice shape.

For instance:

 % 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 have not tested this code, so use it with caution! But that should give an idea.


EDIT

Here it works on your data (also a small bug in the above code has been fixed):

 >> 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 ^^ lines 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 unique slices, but in irregular 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 
+6
source

Very simple and scalable solution:

 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 still required, use the index vector option, as required. %ie, %[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?

+1
source

Source: https://habr.com/ru/post/1469381/


All Articles