Extract X number of elements in 3D matrix based on another matrix
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vesp
el 20 de Jun. de 2017
Editada: Andrei Bobrov
el 23 de Jun. de 2017
Hello,
I have a 3D Matrix A (3x3x5) that is sorted in descending order and another Matrix B (3x3) which has the first X number of elements in Matrix A that I would like to extract and store in a blank matrix C (3x3x5). Matrix B has different first elements to extract, so I would like the rest of the elements in the 3D matrix C to be zero if there isn't anything copied from A. Let's say there are 2 values that I want to extract at element 1x1x1:5, then cell 1x1x1:2 in Matrix C will be 10; 5; 0; 0;0
Example of Matrix A and B: A=randi(10,3,3,5); B=[NaN 2 1; 2 NaN 2;NaN 4 3];
Thank you for the help.
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 20 de Jun. de 2017
Editada: Andrei Bobrov
el 20 de Jun. de 2017
A=randi(10,3,3,5);
B=[NaN 2 1; 2 NaN 2;NaN 4 3];
[m,n,k] = size(A);
B(isnan(B)) = 0;
out = permute(reshape((1:k)'*ones(1,m*n) <= B(:)',[],m,n),[2,3,1]).*A; %2016b and later
out = permute(reshape(bsxfun(@le,(1:k)'*ones(1,m*n),B(:)'),[],m,n),[2,3,1]).*A; %2016a and earlier
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices 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!