Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Arrays in MATLAB

  1. Jun 18, 2013 #1
    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.
     
  2. jcsd
  3. Jun 19, 2013 #2

    kreil

    User Avatar
    Gold Member

    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]
     
     
  4. Jun 19, 2013 #3
    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)
     
  5. Jun 20, 2013 #4

    kreil

    User Avatar
    Gold Member

    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]
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Arrays in MATLAB
  1. Matlab and arrays (Replies: 0)

Loading...