Index notation matlab for 2D array

Click For Summary
SUMMARY

The discussion focuses on understanding index notation in MATLAB for 2D arrays, specifically using the example matrix A defined as A = [1:3; 4:6; 7:9]. It clarifies that A(1:2, 1:2) retrieves the first two rows and columns, while A([3, 1], [3, 1]) accesses specific elements based on the provided indices. The notation [3, 1] creates a row vector, allowing for flexible indexing to form submatrices. The conversation emphasizes the importance of understanding MATLAB's indexing syntax for effective data manipulation.

PREREQUISITES
  • Familiarity with MATLAB syntax and environment
  • Understanding of 2D array structures
  • Knowledge of matrix indexing and slicing techniques
  • Basic programming concepts related to arrays and vectors
NEXT STEPS
  • Explore MATLAB's array manipulation functions
  • Learn about advanced indexing techniques in MATLAB
  • Study the differences between linear indexing and matrix indexing
  • Investigate MATLAB's built-in functions for reshaping arrays
USEFUL FOR

Students, educators, and professionals working with MATLAB who need to enhance their understanding of matrix indexing and data extraction techniques.

gfd43tg
Gold Member
Messages
949
Reaction score
48

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: 716
Last edited:
Physics news on Phys.org
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
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K