How to extract data from a 3D Array?

10 views (last 30 days)
Simon Schirk
Simon Schirk on 18 Apr 2024
Answered: Dyuman Joshi on 18 Apr 2024
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 Comments
Harald
Harald on 18 Apr 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
Simon Schirk on 18 Apr 2024
Sorry, my bad. I'm looking for c). The Data I'm looking for should be displayed as 500 x 5 x n, like the pages in my existing array.

Sign in to comment.

Answers (1)

Dyuman Joshi
Dyuman Joshi on 18 Apr 2024
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)
y =
y(:,:,1) = -0.7285 -0.6881 -0.6747 -0.3796 -0.1672 -0.6574 -0.9762 -0.6068 -0.9793 -0.0795 -0.9972 -0.5266 -0.7477 -0.1073 -0.9397 -0.5630 -0.9243 -0.7642 -0.2448 -0.1465 -0.7908 -0.8282 -0.3473 -0.0134 -0.3137 -0.4403 -0.8926 -0.5689 -0.2770 -0.4045 y(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 y(:,:,3) = -0.0481 -0.3703 -0.8937 -0.2620 -0.6424 -0.1590 -0.3590 -0.7458 -0.8042 -0.6762 -0.5639 -0.4337 -0.4247 -0.3759 -0.6399 -0.7271 -0.9687 -0.0107 -0.3895 -0.5470 -0.2762 -0.8224 -0.4897 -0.7164 -0.5377 -0.7156 -0.8387 -0.6348 -0.3393 -0.1025 y(:,:,4) = -0.7409 -0.3158 -0.9468 -0.7263 -0.4370 -0.7029 -0.3200 -0.8704 -0.2631 -0.8824 -0.4113 -0.4293 -0.5657 -0.7926 -0.9319 -0.6440 -0.1914 -0.1186 -0.0940 -0.8801 -0.3449 -0.3131 -0.0717 -0.8211 -0.6848 -0.8336 -0.2107 -0.4389 -0.7302 -0.3056 y(:,:,5) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
%Check if there is any non-zero element in a page
idx = any(y, [1 2])
idx = 1x1x5 logical array
idx(:,:,1) = 1 idx(:,:,2) = 0 idx(:,:,3) = 1 idx(:,:,4) = 1 idx(:,:,5) = 0
%Use the (logical) index to get the corresponding data
out = y(:, :, idx)
out =
out(:,:,1) = -0.7285 -0.6881 -0.6747 -0.3796 -0.1672 -0.6574 -0.9762 -0.6068 -0.9793 -0.0795 -0.9972 -0.5266 -0.7477 -0.1073 -0.9397 -0.5630 -0.9243 -0.7642 -0.2448 -0.1465 -0.7908 -0.8282 -0.3473 -0.0134 -0.3137 -0.4403 -0.8926 -0.5689 -0.2770 -0.4045 out(:,:,2) = -0.0481 -0.3703 -0.8937 -0.2620 -0.6424 -0.1590 -0.3590 -0.7458 -0.8042 -0.6762 -0.5639 -0.4337 -0.4247 -0.3759 -0.6399 -0.7271 -0.9687 -0.0107 -0.3895 -0.5470 -0.2762 -0.8224 -0.4897 -0.7164 -0.5377 -0.7156 -0.8387 -0.6348 -0.3393 -0.1025 out(:,:,3) = -0.7409 -0.3158 -0.9468 -0.7263 -0.4370 -0.7029 -0.3200 -0.8704 -0.2631 -0.8824 -0.4113 -0.4293 -0.5657 -0.7926 -0.9319 -0.6440 -0.1914 -0.1186 -0.0940 -0.8801 -0.3449 -0.3131 -0.0717 -0.8211 -0.6848 -0.8336 -0.2107 -0.4389 -0.7302 -0.3056

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!