frequency of 2D matrix in 3D matrix

1 visualizzazione (ultimi 30 giorni)
AMRIT JETHI
AMRIT JETHI il 7 Mar 2019
Modificato: Jos (10584) il 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??

Risposte (1)

Jos (10584)
Jos (10584) il 7 Mar 2019
Modificato: Jos (10584) il 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, ..)) , ..)), ..)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by