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