2. MATLAB MATRICES AND VECTORS

2.1 Dense matrices and vectors
2.2 Sparse matrices and vectors
2.3 Range operators
2.4 Grid operators
2.5 Size and shape operators
2.6 Special matrices

Matlab "thinks" in vector's and matrices, and it is most efficient if Matlab users treat every variable as a vector or a matrix. I will followup on this latter. We will start by defining some vectors and some matrices.

To practice these commands, either cut and paste or type something similar in you Matlab window.


2.1 Define a dense matrix or vector


Define a matrix A -- use [] to signify the start and end. Use ; to signify the row breaks

>> A = [ 1 2; 3 4] 

A =

     1     2
     3     4


Define a matrix A silently -- note the ; at the end of the line

>> A = [ 1 2; 3 4];


Define a row vector b

>> b = [1 2 3 4]

b =

     1     2     3     4


Transpose it to a column vector -- use '

>> b = [1 2 3 4]'

b =

     1
     2
     3
     4


Enter it as a column vector

>> b = [1; 2; 3; 4]

b =

     1
     2
     3
     4


Change an element

>> b(1) = 4

b =

     4
     2
     3
     4


The current variables are;

>> whos
  Name      Size         Bytes  Class

  A         2x2             32  double array
  b         4x1             32  double array

Grand total is 8 elements using 64 bytes


2.2 Define a sparse matrix or vector

A sparse matrix or vector usually has many zero entries


Convert from a dense matrix

>> A = sparse(A)

A =

   (1,1)        1
   (2,1)        3
   (1,2)        2
   (2,2)        4


Enter it directly

>> A(1,1) = 2

A =

   (1,1)        2
   (2,1)        3
   (1,2)        2
   (2,2)        4


2.3 Range operator ":"

The range operator allows one to set up a vector of equally spaced entries.


Defining vectors with the range operator

>> x = 1:4

x =

     1     2     3     4

    
>> x = 1:2:6

x =

     1     3    5


Accessing data from arrays with the range operator

>> A = [ 1 2; 3 4];    
>> A(1,:)

ans =

     1     2



>> A(:,1)

ans =

     1
     3


>> A = rand(4), x = 2:3

A =

    0.9501    0.8913    0.8214    0.9218
    0.2311    0.7621    0.4447    0.7382
    0.6068    0.4565    0.6154    0.1763
    0.4860    0.0185    0.7919    0.4057


x =

     2     3


>> A(x,x)

ans =

    0.7621    0.4447
    0.4565    0.6154


2.4 Grid definition operators


Creating meshes of two dimensional coordinates

>> [x,y] = meshgrid(1:2,1:3)

x =

     1     2
     1     2
     1     2


y =

     1     1
     2     2
     3     3


2.5 Size and shape operators

These operators access basic array information


Find the length of a vector

>> b = [1 2 3 4]
>> length(b)

ans =

     4


Find the size of a matrix

>> A = [ 1 2; 3 4];
>> size(A)

ans =

     2     2


Reshape a matrix

>> A = [ 1 2; 3 4];
>> reshape(A,4,1)

ans =

     1
     3
     2
     4


Find the number of dimensions of a matrix

>> A = [ 1 2; 3 4];
>> ndims(A)

ans =

     2


2.6 Special matrices

If you want a square matrix, these routines require only one size argument. If you want a rectangular matrix, you must supply the dimensions.


A matrix of all zeros

>> A = zeros(4,2)

A =

     0     0
     0     0
     0     0
     0     0


A matrix of all ones

>> A = ones(4)

A =

     1     1     1     1
     1     1     1     1
     1     1     1     1
     1     1     1     1


A matrix of uniformly distribed random numbers on (0,1)

>> A = rand(4)

A =

    0.2190    0.9347    0.0346    0.0077
    0.0470    0.3835    0.0535    0.3834
    0.6789    0.5194    0.5297    0.0668
    0.6793    0.8310    0.6711    0.4175


An identity matrix

>> A = eye(4)

A =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


David Eyre
9/8/1998