Index notation matlab for 2D array

In summary, the notation used in Matlab is used to define subsets of an array. The notation [a, b] defines a row vector and A([a, b], [c, d]) creates a 2x2 matrix with elements from the specified rows and columns.
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


Homework Equations


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:
  A = [1:3; 4:6; 7:9]

A =

     1     2     3
     4     5     6
     7     8     9

Code:
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:
 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?
 

Attachments

  • 2D arrays indexing.jpg
    2D arrays indexing.jpg
    27 KB · Views: 635
Last edited:
Physics news on Phys.org
  • #2
Maylis said:
Code:
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.
It defines the subset of an array. Say you want the first column of the matrix, then you write
Code:
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".

Maylis said:
Code:
 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?
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:
A([3,1],[2,3,1,2])
ans =

   8   9   7   8
   2   3   1   2
 

1. What is index notation in MATLAB?

Index notation in MATLAB is a way of accessing and manipulating elements in a multidimensional array. It uses numeric values to specify the position of an element in the array.

2. How do I create a 2D array in MATLAB?

A 2D array can be created in MATLAB by using the "zeros" or "ones" function, or by manually inputting values into a matrix using square brackets.

3. How do I access specific elements in a 2D array using index notation?

To access an element in a 2D array, you need to specify the row and column index separated by a comma. For example, array(2,3) would access the element in the second row and third column of the array.

4. Can index notation be used to modify elements in a 2D array?

Yes, index notation can be used to modify elements in a 2D array. Simply assign a new value to the specified index to change the element.

5. Are there any other ways to access elements in a 2D array besides index notation?

Yes, besides index notation, you can also use logical indexing, which involves creating a logical array to specify which elements you want to access. You can also use the "end" keyword to access the last element in a particular dimension.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
996
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top