Extracting data from non-uniform levels in 3D array

2 visualizaciones (últimos 30 días)
Bryan
Bryan el 12 de Sept. de 2012
I have been trying to do this for a week and have had no hope. Maybe somebody has done it before and has some tips!
I have two 3-D arrays
Array "A": 207x177x18 array. It is a temperature data raster with 18 vertical levels.
Array "B": 207x177x18 array of zeros, with 1 values at the vertical level I am interested in for each raster point.
I want to use Array B as a mask for Array A, so that I get the 2D Matrix "C", a 207x177 raster with only the data from the vertical level I am interested in.
Any tips would be appreciated!!

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 12 de Sept. de 2012
Editada: Sean de Wolski el 12 de Sept. de 2012
Assuming that each row/col position of B has exactly one 1 throughout its depth, then this can be done like follows:
%An A
A = repmat(magic(10),[1 1 5]);
%Simulate a B where each row/col pair has exactly one true value through
%depth
B = false(size(A));
[~,idx] = max(rand(size(A)),[],3);
[rr, cc] = ndgrid(1:size(A,1),1:size(A,2));
B(sub2ind(size(B),rr(:), cc(:), idx(:))) = true;
%Check it
assert(all(all(sum(B,3)==1)))
%Now, how do we undo the above?
[~,idx] = max(B,[],3); %which page?
C =reshape(A(sub2ind(size(B),rr(:), cc(:), idx(:))),size(idx)); %rr/cc from above, reshape to original shape
%Check it
assert(isequal(C,magic(10)));
And of course doc sub2ind will be your friend.
  1 comentario
Bryan
Bryan el 13 de Sept. de 2012
Thanks very much! It seems to work! Now I will teach myself exactly what it is you have done :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by