4. Colon Notation

MATLAB also offers some very powerful ways for creating vectors and then using this means to tear a matric apart. Try

x=-2:1
length(x)
-2:.5:1
-2:.2:1
a=magic(5)
a(2,3)

Now we will use the colon notation to get a column of a.

a(2,:)
a(:,3)
a
a(2:4,:)
a(:,3:5)
a(2:4,3:5)
a(1:2:5,:)

You can put a vector in the row or column position of a.

a(:,[1, 2, 5])
a([2, 5], [2, 4, 5])

You can also make assignment statements using a vector or a matrix.

b=magic(5)
b([1 2],:)=a([1 2],:)
a(:,[1 2])=b(:,[3 5])
a(:,[1 5])=a(:,[5 1])
a=a(:,5:-1:1)

When you insert a 0-1 vector into the column position, the columns which correspond to the 1's are displayed. Try

v=[0 1 0 1 1]
a(:,v)
a(v,:)


Next Section: 5. Programming in Matlab


Created by F.G. Garvan (fgarvan@ufl.edu) on Tue, Sep 30, 97
Last update made Thu Oct 2 11:22:16 EDT 1997.
Back to Table of Contents
EMAIL: fgarvan@ufl.edu fgarvan@ufl.edu