How to extract data from a 3D Array?
Mostrar comentarios más antiguos
I have a large (500x5x79039) 3D array, which is mostly filled with zeros. Now I want to extract the slices with content into a smaller array and save them. I’ve already tried the solution from this Article, but then I get the same page repeated multiple times, resulting in too many entries and duplicates.
Ideally, I would like to extract the pages exactly as they were saved, provided there is data on those pages.
4 comentarios
Jonas
el 18 de Abr. de 2024
so a slice is a 2d matrix for me, into which direction do you want to slice? if i understood correctly, you only want those 2d slices, in whch at least one entry is not zero?
Simon Schirk
el 18 de Abr. de 2024
Harald
el 18 de Abr. de 2024
Jonas inquired about the direction of indexing. In other words, you can get
a) a n x 5 x 79039 array with n < 500
b) a 500 x n x 79039 array with n < 5
c) a 500 x 5 x n array with n < 79039
Each can be done, but it would be great to know which of the three you are looking for.
Simon Schirk
el 18 de Abr. de 2024
Respuestas (1)
Use logical indexing -
%Sample data
p = 5;
q = 6;
r = 4;
y = rand(p,q,r)-1;
y(:, :, [2 r+1]) = zeros(p,q,2)
%Check if there is any non-zero element in a page
idx = any(y, [1 2])
%Use the (logical) index to get the corresponding data
out = y(:, :, idx)
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!