Organizing & Extracting Data from a Large Array

  • Context: MATLAB 
  • Thread starter Thread starter PaulaS
  • Start date Start date
  • Tags Tags
    Array Data
Click For Summary

Discussion Overview

The discussion revolves around the problem of organizing and extracting data from a large array in a programming context. Participants explore methods to create subarrays based on the values in the first column of the array, with a specific focus on handling a large dataset containing over 12,000 rows and up to 74 subarrays.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant presents an array and expresses the need to extract rows based on the first column's values, aiming to create separate subarrays for each unique starting number.
  • Another participant suggests using a loop to create a cell array that stores the subarrays, providing a code snippet as an example.
  • A later reply indicates that the initial implementation worked for the first subarray but encountered an issue with an empty matrix for subsequent subarrays, suggesting that no entries matched the queried starting number.
  • Further clarification is provided about the behavior of the logical indexing used in the extraction process, explaining why an empty result occurs when there are no matching entries in the original array.

Areas of Agreement / Disagreement

Participants generally agree on the method of using loops and logical indexing to extract subarrays, but there is a lack of consensus on how to handle cases where no entries match the specified criteria, leading to empty matrices.

Contextual Notes

Participants note that the original array has a significant number of rows and columns, which may affect performance and the handling of edge cases, such as when no data matches the specified conditions.

PaulaS
Messages
18
Reaction score
0
Hello there,

Let's say I have the following array:
(what interests me in the array is the first column)

1 5 9
1 4 9
1 8 9
1 9 6
2 5 6
2 4 8
2 4 7
2 6 9
2 3 0
2 0 4
3 5 8
3 4 8
3 9 8
4 8 5
4 8 9
4 8 5
4 3 9
4 8 7
5 8 9
5 8 9
5 4 8
5 7 8
5 6 2
... and so on

The first columns starts with either 1 or 2 or 3 or 4 or 5.

The thing that I want to do is to extract all the rows that start with 1, separately along with all the rows that start with 2, and so on ...

So in the end, according to the above example, I'll have 5 'sub'arrays:
The first is for the columns that start with 1.
The second is for the columns that start with 2.
and so on ...

So this is it.

HOWEVER, my array has 12332 rows and I will have more than 20 'subarrays'. Plus, I want to name each one differently, since later on I'll have to call each subarray on its own.

How can I do it?

I hope I made it as clear as possible.
 
Physics news on Phys.org
Code:
A = [1 5 9; 1 4 9; 1 8 9; 1 9 6; 2 5 6;2 4 8 ;2 4 7 ;2 6 9;2 3 0 ;2 0 4;3 5 8;3 4 8;3 9 8;4 8 5;4 8 9;4 8 5;4 3 9 ;4 8 7;5 8 9;5 8 9;5 4 8;5 7 8;5 6 2];
A(A(:,1)==1,:)

ans =

     1     5     9
     1     4     9
     1     8     9
     1     9     6

If your numbers range from 1-20, then you'll just need a loop. You can store the results in a cell array for use later:

Code:
for i = 1:20
C{i}= A(A(:,1)==i,:);
end

When you want to retrieve a matrix with a column that starts with a certain number from the cell array, call it from C using curly brackets:

Code:
C{3}

ans =

     3     5     8
     3     4     8
     3     9     8

If you use regular parentheses it will just give some info about the size:

Code:
C(3)

ans = 

    [3x3 double]
 
I tried it out. But I have 12,332 rows and these rows will contain 74 subarrays (I said earlier more than 20, it turned out 74 subarrays)

I tried out your code, the first array was right, however the second one I got the following:
'Empty matrix: 0-by-7'

(0 by 7 since my original array has 7 columns)
 
That means that there are no entries that start with that number. For example, your example array above ranges 1:5 in the first column. If I loop i=1:20 and call C{6}:
Code:
C{6}

ans =

   Empty matrix: 0-by-3
It comes up empty because the call it is evaluating is: A(A(:,1)==6,:). The inside part, A(:,1)==6, evaluates to 0 everywhere since nothing starts with a 6. Then this is passed as a logical index into A( ,:), but since it is all zeros, it returns the empty array (the colon gives the second dimension nonzero extent).You can test whether a cell in your cell array is empty by using isempty(), or just indexing with parentheses:
Code:
isempty(C{7})

ans =

     1

C(7)

ans = 

    [0x3 double]
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
1K
Replies
7
Views
2K
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K