MATLAB Organizing & Extracting Data from a Large Array

  • Thread starter Thread starter PaulaS
  • Start date Start date
  • Tags Tags
    Array Data
AI Thread Summary
The discussion focuses on extracting rows from a large array based on the first column's values, specifically separating them into subarrays for each unique starting number. The user has an array with over 12,000 rows and needs to create 74 subarrays, each named differently for later access. A suggested approach involves using a loop to filter the original array and store the results in a cell array. However, issues arise when trying to access subarrays that correspond to numbers not present in the first column, resulting in empty matrices. The solution includes using the `isempty()` function to check for empty subarrays before attempting to access them.
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

Back
Top