# Arrays in MATLAB

1. Jun 18, 2013

### PaulaS

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:
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.

2. Jun 19, 2013

### kreil

Code (Text):

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 (Text):

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 (Text):

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 (Text):

C(3)

ans =

[3x3 double]

3. Jun 19, 2013

### PaulaS

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)

4. Jun 20, 2013

### kreil

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 (Text):

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 (Text):

isempty(C{7})

ans =

1

C(7)

ans =

[0x3 double]