accessing indexed values in a 3D array with a logical index
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Belinda Finlay
el 24 de Jul. de 2020
Comentada: Belinda Finlay
el 24 de Jul. de 2020
I have 3D array of temperature data that looks like this:
Z = rand(3,4,8);
It contains 8 slices of data on 3*4 grid. I have a logical array which is an index to a set of values on each of the 8 slices like this:
ind = logical(randi(2, [3 4]) - 1)
How to I create a new matrix which is the 3D and contains the temp data from Z for ind on each of the slices.
My Z matrix is actally 814*1294*40 and looking at other answers there should be a way to do this; however I am stumped.
0 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Jul. de 2020
ind = repmat(ind, 1, 1, size(Z,3));
Z(ind)
However, this will give you a vector of results.
How to I create a new matrix which is the 3D and contains the temp data from Z for ind on each of the slices.
That is a problem, because when you select values for each slice, you get holes, and there is no way to create arrays with holes.
You could reshape Z(ind) to [], 1, size(Z,3) which would give you a vector for each layer.
7 comentarios
Walter Roberson
el 24 de Jul. de 2020
Let ind be the rectangular 2D mask that you had from before. Then
mask = any(ind,1);
first_col = find(mask,1);
last_col = find(mask,1,'last');
mask = any(ind,2);
first_row = find(mask,1);
last_row = find(mask,1,'last');
selected_area = Z(first_row : last_row, first_col : last_col, :);
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays 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!