How to extract data from a 3D Array? (2024)

19 views (last 30 days)

Show older comments

Simon Schirk about 20 hours ago

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array

Answered: Dyuman Joshi about 12 hours ago

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

Show 2 older commentsHide 2 older comments

Jonas about 14 hours ago

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3135971

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 about 12 hours ago

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3136111

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3136111

Yes, exactly.

Harald about 12 hours ago

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3136151

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3136151

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 about 12 hours ago

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3136211

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#comment_3136211

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.

Sign in to answer this question.

Answers (1)

Dyuman Joshi about 11 hours ago

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#answer_1443956

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2108586-how-to-extract-data-from-a-3d-array#answer_1443956

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.4045y(:,:,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 0y(:,:,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.1025y(:,:,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.3056y(:,:,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) = 1idx(:,:,2) = 0idx(:,:,3) = 1idx(:,:,4) = 1idx(:,:,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.4045out(:,:,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.1025out(:,:,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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Tags

  • matlab
  • find
  • array
  • arrays
  • cell array
  • cell arrays

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to extract data from a 3D Array? (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How to extract data from a 3D Array? (2024)
Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6023

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.