1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Index notation matlab for 2D array

  1. Jun 12, 2014 #1

    Maylis

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
    Hello,

    I am having some confusion over the notation used in matlab. I don't really know what they mean

    Code (Text):
      A = [1:3; 4:6; 7:9]

    A =

         1     2     3
         4     5     6
         7     8     9
     
    Code (Text):
    A(1:2, 1:2)

    ans =

         1     2
         4     5
     
    I clearly see that this is giving me the 1st row, 2nd column and 2nd row, 2nd column. I don't see how (1:2, 1:2) signifies that though.

    Code (Text):
     A([3, 1], [3, 1])

    ans =

         9     7
         3     1
     
    similarly, how is this one showing the 3rd row, 3rd column and 1st row, 1st column backwards? What does the [3, 1], [1, 3] mean?
     

    Attached Files:

    Last edited: Jun 12, 2014
  2. jcsd
  3. Jun 12, 2014 #2

    DrClaude

    User Avatar

    Staff: Mentor

    It defines the subset of an array. Say you want the first column of the matrix, then you write
    Code (Text):
    A(1:3,1)
    ans =

       1
       4
       7
     
    which means "take rows 1 to 3 for column 1". This can also be achieved using A(:,1), which means "take all rows for column 1". A(1:2,1:2) means "take rows 1 to 2 and columns 1 to 2".

    The notation [3, 1] defines a row vector. What A([a, b], [c, d]) does is that it makes a 2x2 matrix with elements
    A(a,c) A(a,d)
    A(b,c) A(b,d)

    This also works with longer vectors:
    Code (Text):
    A([3,1],[2,3,1,2])
    ans =

       8   9   7   8
       2   3   1   2
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Index notation matlab for 2D array
  1. Concatenating 2D array (Replies: 1)

Loading...