frequency of 2D matrix in 3D matrix

1 view (last 30 days)
AMRIT JETHI
AMRIT JETHI on 7 Mar 2019
Edited: Jos (10584) on 7 Mar 2019
I have a 3D matrix of size 100X100X40. That means I have 40 100X100 2D matrices. I want to know the number of occurances of each 2D matrix in the 3D matrix.
This is somewhat similar to the unique command we have for elements in 1D array.
How to implement it for 2 D matrices??

Answers (1)

Jos (10584)
Jos (10584) on 7 Mar 2019
Edited: Jos (10584) on 7 Mar 2019
Convert the 3D N-by-M-by-Z matrix into a 2D Z-by-(N*M) matrix and use unique with the rows option on that. An example:
M3D = cat(3, [1 1 ; 1 1], [1 1; 1 1], [2 2 ; 2 2], [1 1 ; 1 1])
M2D = reshape(M3D, [], size(M3D, 3)).'
[~, i] = unique(M2D, 'rows') ;
Mun3D = M3D(:,:,i)
% which you can also do in an incomprehensible one-liner ;-)
% Mun3D = reshape(transpose(unique(transpose(reshape(M3D, ..)) , ..)), ..)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!