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

35 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Simon Schirk am 18 Apr. 2024 um 8:50

  • Verknüpfen

    Direkter Link zu dieser Frage

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

  • Verknüpfen

    Direkter Link zu dieser Frage

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

Beantwortet: Dyuman Joshi am 18 Apr. 2024 um 16:45

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 Kommentare

2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden

Jonas am 18 Apr. 2024 um 13:34

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Simon Schirk am 18 Apr. 2024 um 15:22

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Yes, exactly.

Harald am 18 Apr. 2024 um 15:32

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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 am 18 Apr. 2024 um 16:01

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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.

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (1)

Dyuman Joshi am 18 Apr. 2024 um 16:45

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

In MATLAB Online öffnen

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 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Tags

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

Produkte

  • MATLAB

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


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

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

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

Europa

  • 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)

Asien-Pazifik

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Kontakt zu Ihrer lokalen Niederlassung

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

FAQs

How do you extract elements from an array? ›

In the Indices field, specify one or more zero-based elements to extract from the input array. For 1-D arrays, this is the subscript of the array element. For example, if the array is [1, 2, 3, 4, 5, 6] and you want to extract the third element (with a value of 3), enter an Indices value of 2.

How to get 2D array from 3D array in Python? ›

Reshaping a 3D Array to a 2D Array

We can use the numpy. reshape() function for this. The reshape() function allows us to reorganize the array elements, given new dimensions.

How to extract values from an array in Python? ›

  1. To extract values out of an array in Python using only a string as input, you need to convert the string to a format that allows indexing, such as a list or an array. ...
  2. ```python.
  3. # Input string containing indices.
  4. input_string = "1 3 5"
  5. # Sample array.
  6. my_array = [10, 20, 30, 40, 50, 60]
May 11, 2023

How do you access data elements in an array? ›

Access Array Elements

You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How do you access elements in an array list? ›

Access ArrayList Elements

In the above example, we have used the get() method with parameter 1 . Here, the method returns the element at index 1. To learn more, visit the Java ArrayList get(). We can also access elements of the ArrayList using the iterator() method.

How to get values from a 3D array in Python? ›

The following code creates a 3-dimensional array:
  1. # a 3D array, shape-(2, 2, 2) >>> d3_array = np. ...
  2. # retrieving a single element from a 3D-array >>> d3_array[0, 1, 0] 2.
  3. # retrieving a 1D sub-array from a 3D-array >>> d3_array[:, 0, 0] array([0, 4])

How to access 3D arrays? ›

Accessing elements in 3D Arrays is also similar to that of 2D Arrays. The difference is we have to use three loops instead of two loops for one additional dimension in 3D Arrays. where, x: Index of 2D array.

How to access 3D list in Python? ›

Using nested for loops:

We will start by creating an empty list and then using three for loops to iterate over the dimensions and append the '#' character to the list. Create an empty list lst to hold the 3D list. Loop through the range x to create the first dimension of the 3D list.

How to convert a 3D array into a 2D array? ›

reshape() function. This function takes in the original 1D array and the desired shape of the new 2D array as parameters. The new shape must have the same number of elements as the original array. If the new shape does not have the same number of elements, an error will be raised.

How to convert 3D list to 2D list in Python? ›

ALGORITHM:
  1. Define a function flatten_list(lst) that takes a nested list lst as input.
  2. Initialize an empty list result to store the flattened list.
  3. Loop through each element in lst.
  4. If the element is a list, recursively call flatten_list function and append its elements to result.
Apr 12, 2023

Can you have a 3D array in Python? ›

In Python, you can create a 3D array using the NumPy library. Here's an example code snippet that creates a 3D array of size 2x3x4: pythonCopy codeimport numpy as np. # create a 3D array of size 2x3x4.

How do you extract a single element from an array in Python? ›

Method 1: Using Slicing

One way to remove the first element from an array in Python is by using slicing. Slicing is a technique that allows you to extract a portion of a sequence, such as a list or an array, by specifying the start and end indices.

How to extract data from a list in Python? ›

Step-by-step approach:
  1. Initialize the original list and K value.
  2. Create an empty list to store the extracted elements.
  3. Loop through the list using the enumerate() function to access both the index and value of each element in the list.
May 10, 2023

How to extract values from an array in numpy? ›

Explanation
  1. In line 1, we import the numpy package.
  2. In line 4, we create an array and specify the number of elements to be 10 , ranging from 1 to 10.
  3. In line 7, we specify two conditions directly to the array. Each condition is in the form of a tuple . ...
  4. In line 8, we print the extracted elements.

How to extract elements from an array in JavaScript? ›

array. slice(start, end); Example 1: In this example, we have an array arr and we want to extract the first 3 elements from it. So, we used the slice() method with a start index of 0 and an end index of 3, which extracts the first 3 elements.

How to retrieve an element from an array in JavaScript? ›

The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. If you need the index of the found element in the array, use findIndex() .

References

Top Articles
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5492

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.