Arrays and vector

matlab is designed to help you manipulate very large sets of numbers quickly and with minimal programming. Operations on numbers can be done efficiently by storing them as vectors/matrices. matlab is particularly good at doing matrix operations.


matlab can do all vector operations completely painlessly. Try the following commands
>> a = [6,3,4]
a =
      6 3 4
>> a(1)
ans =
         6
>> a(2)
ans =
          3
>> a(3)
>> b = [3,1,-6]
>> c = a + b
c =
       9   4   -2
>> c = dot(a,b)
c =
       -3
>> c = cross(a,b)
c =
       -22   48   -3

Calculate the magnitude of c (you should be able to do this with a dot product. matlab also has a built-in function called `norm’ that calculates the magnitude of a vector). A vector in matlab need not be three dimensional. For example, try
>> norm(c)
ans =
          52.8867
>> a = [9,8,7,6,5,4,3,2,1]
a =
          9   8   7   6   5   4   3   2   1
>> b = [1,2,3,4,5,6,7,8,9]
You can add, subtract, and evaluate the dot product of vectors that are not 3D, but you can’t take a cross product. Try the following
>> a + b
ans =
         10   10   10   10   10   10   10   10   10

In matlab, vectors can be stored as either a row of numbers, or a column of numbers. So you could also enter the vector a as
>> a = [9;8;7;6;5;4;3;2;1]
a =
       9
       8
       7
       6
       5
       4
       3
       2
       1

to produce a column vector.
You can turn a row vector into a column vector, and vice-versa by
>> b = transpose(b)

The statement
                          t = a : h : b;
with h>0 creates a row vector of the form
                         t = [ a, a+h, a+2h, ... ]
giving all values a+jh that <= b.
         When h is omitted, it is assumed to be 1. Thus,
                        n = 1 : 5
creates the row vector
                        n = [,1,2,3,4,5]


A few more very useful vector tricks are as follows.

You can create a vector containing regularly spaced data points very quickly with a loop. Try
>> for i=1:11 v(i)=0.1*(i-1); end
>> v
v =
Columns 1 through 7

0 0.1000   0.2000   0.3000   0.4000   0.5000   0.6000

Columns 8 through 11

0.7000   0.8000   0.9000   1.0000

 

The for…end loop repeats the calculation with each value of i from 1 to 11. Here, the “counter” variable i now is used to refer to the i-th entry in the vector v, and also is used in the formula itself.

As another example, suppose you want to create a vector v of 101 equally spaced points, starting at 3 and ending at 2*pi, you would use

>> for i=1:101 v(i)= 3 + (2*pi-3)*(i-1)/100; end
>> v
If you type
>> sin(v)

matlab will compute the sin of every number stored in the vector v and return the result as another vector. This is useful for plots.

You have to be careful to distinguish between operations on a vector (or matrix, see later) and operations on the components of the vector. For example, try typing

>> v^2
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

The correct input should be
>> v.^2

(there is a period after the v, and no space). This squares every element within v. You can also do things like

>> v. /(1+v)

To avoid dot notation---mostly because it makes code hard to read---use loops. For example, instead of writing w = v.^2, you may want to use
>> for i=1:length(v) w(i) = v(i)^2; end

Here, ‘for i=1:length(v)’ repeats the calculation for every element in the vector v. The function length(vector) determines how many components the vector v has (101 in this case). Using loops is not elegant programming, and slows down matlab.

Hopefully you know what a matrix is… If not, it doesn’t matter - for now, it is enough to know that a matrix is a set of numbers, arranged in rows and columns---see for detail matlab Manual for the second course.

matlab works very efficiently with arrays, and any tasks are best done with arrays. The statement

t = a:h:b

with h>0 creates a row vector of the form

t = [ a, a+h, a+2h, ... ]

giving all values a+jh that are <=b. When h is omitted, it is assumed to be 1. Thus,

n = 1:5

creates the row vector

n= [1,2,3,4,5]

 

II. Special Ararys


                          A = zeros(2,3)
produces an array with 2 rows and 3 columns, with all entries set to zero.
                          B = ones(3,2)
produces an array with 3 rows and 2 columns, with all components set to 1.
                          eye(3)
results in the 3x3 identity matrix.