Extract 2D array from 3D array using logical index
30 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Leo Pio D'Adderio
el 31 de Oct. de 2024 a las 11:36
I have a PxMxN array that I want to convert in a PxK 2D array. K has to be obtained from a logical matrix MxN. Consequently, numel(K)<=numel(MxN). Can anyone help me? Thanks.
0 comentarios
Respuestas (1)
Stephen23
el 31 de Oct. de 2024 a las 14:14
Editada: Stephen23
el 31 de Oct. de 2024 a las 14:31
"I have a PxMxN array that I want to convert in a PxK 2D array. K has to be obtained from a logical matrix MxN. Consequently, numel(K)<=numel(MxN). Can anyone help me?"
Just use the indexing and then RESHAPE (which does not move any data in memory so is very efficient):
format compact
A = randi(9,5,4,3)
X = randi(0:1,4,3);
X = logical(X)
B = A(:,X); % easy indexing
B = reshape(B,size(A,1),[])
Checking the first of the index values:
[R,C] = find(X,1,'first')
A(:,R,C)
This works because MATLAB applies the final index to all trailing dimensions:
An interesting side-effect of this is that linear indexing is really just subscript indexing with one index.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!