GEOSC 444
Matlab Application for Geoscience

Lesson 2: More complicated vector and array addressing

PrintPrint

Syntax introduced:

diag reshape

We've already practiced using parentheses to address a certain element of a vector. You can also use that technique to address a specific spot in a matrix. For example, v(1) addresses the first element in a vector v. M(1,1) addresses the element in the top left corner of the matrix M. In the example below I make a 3x3 matrix M. Then I ask it for the element in the second row and third column. It's just like playing Battleship except both the columns and rows are designated by numbers.

>> M=[1 2 3; 4 5 6; 7 8 9];
M =
     1     2     3

     4     5     6

     7     8     9

>> M(2,3) 
ans = 
     6

Use the colon operator to address a range of elements in a vector or matrix. For example, v(:) addresses all the elements of a vector, v(a:b) addresses elements a through b in vector v. M(:,a) addresses column a, M(a,:) addresses row a, M(:,a:b) addresses columns a through b, M(a:b,:) addresses rows a through b, M(a:b,c:d) addresses the intersection of rows a through b and columns c through d.

>> M(:,2)
ans =
     2

     5

     8
>> M(3,:) 
ans = 
     7     8     9 
>> M(1:2,2:3) 
ans = 
     2     3

     5     6

Use a square bracket to address nonconsecutive elements in a vector or matrix. For example v([a,b,c:d]) addresses elements a, b, and c through d. M([a,b],[c:d,e]) addresses the intersection of rows a and b and columns c through d and e.

>> M([1,3],1:3)
ans =
     1     2     3

     7     8     9

Other games you can play

To append an element to a vector just specify a value at the desired position. If it is not the next consecutive position, MATLAB pads the elements in between with zeros. You can append existing vectors to each other if they are all row vectors or all column vectors.

>> v=1:20;
>> v(25)=7.6
v =
  Columns 1 through 9
    1.0000    2.0000    3.0000    4.0000    5.0000    6.0000    7.0000    8.0000    9.0000
  Columns 10 through 18
   10.0000   11.0000   12.0000   13.0000   14.0000   15.0000   16.0000   17.0000   18.0000
  Columns 19 through 25
   19.0000   20.0000         0         0         0         0    7.6000

To append vectors to a matrix you need to make sure the dimensions work out so that all rows have the same number of elements.

Use empty brackets to delete an element from a vector or a row/column from a matrix. Deleting is not the same as assigning zero to the value of that element. Using empty brackets to delete elements from a matrix works if you are going to delete a whole row or a whole column, but not just one element. See in the snippet below a successful deletion of the fourth element of a vector, and what happens when I try to delete just one element from a 4x3 matrix.

>> v=[1:5];
>> v(4)=[]
v =
     1     2     3     5
>> M=ones(4,3);
>> M(1,1)=[]
A null assignment can have only one non-colon index.

diag on a vector creates a matrix whose diagonal is the initial vector and whose other elements are zero. With a matrix, diag pulls out the diagonal elements and makes a vector out of them.

>> v=1:5
v =
     1     2     3     4     5
>> diag(v)
ans =
     1     0     0     0     0
     0     2     0     0     0
     0     0     3     0     0
     0     0     0     4     0
     0     0     0     0     5
>> diag(ans)
ans =
     1
     2
     3
     4
     5

reshape has syntax reshape(M,a,b). It takes a matrix M that used to have x rows and y columns and turns it into a matrix with a rows and b columns. a*b must equal x*y for this to work.