How to get the unique slices of a matrix?
built-in, language-features, matlab, matrix
Solution
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
Problem
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 ```