Octave/Matlab  - Marix                                                             Home : www.sharetechnote.com

 

 

 

In this page, I would post a quick reference for Matlab and Octave. (Octave is a GNU program which is designed to provide a free tool that work like Matlab. I don't think it has 100% compatability between Octave and Matlab, but I noticed that most of basic commands are compatible. I would try to list those commands that can work both with Matlab and Octave). All the sample code listed here, I tried with Octave, not with Matlab.

 

There are huge number of functions which has not been explained here, but I would try to list those functions which are most commonly used in most of matlab sample script you can get. My purpose is to provide you the set of basic commands with examples so that you can at least read the most of sample script you can get from here and there (e.g, internet) without overwhelming you. If you get familiar with these minimal set of functionality, you would get some 'feeling' about the tool and then you would make sense out of the official document from Mathworks or GNU Octave which explains all the functions but not so many examples.

 

I haven't completed 'what I think is the minimum set' yet and I hope I can complete within a couple of weeks. Stay tuned !!!

 

Matrix (Two or Higher Dimmensional Array)

 

 

< Creating a Matrix >

 

Method 1 :  m = [ value value value ; value value value ;  ...]

 

Ex)

Input

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

Output

 m =

   1   2   3

   4   5   6

   7   8   9

 

 

Method 2 :  m = [ vector ; vector ;  ...]

 

Ex)

Input

v1 = [1 2 3];

v2 = [4 5 6];

v3 = [7,8,9];

m = [v1; v2; v3]

Output

 m =

   1   2   3

   4   5   6

   7   8   9

 

 

< Creating an Identity Matrix >

 

Method 1 :  m = eye(M) // M x M identity matrix

 

Ex)

Input

 m = one(3)

Output

 m =

   1   0   0

   0   1   0

   0   0   1

 

 

< Creating M x N Zero Matrix >

 

Method 1 :  m = zeros(M,N)

 

Ex)

Input

 m = zeros(4,2)

Output

 m =

   0   0

   0   0

   0   0

   0   0

 

 

Method 2 :  m = zeros(M) // M x M square matrix

 

Ex)

Input

 m = zeros(3)

Output

 m =

   0   0   0

   0   0   0

   0   0   0

 

 

< Creating a M x N one matrix >

 

Method 1 :  m = ones(M,N)

 

Ex)

Input

 m = ones(4,2)

Output

 m =

   1   1

   1   1

   1   1

   1   1

 

 

Method 2 :  m = ones(M) // M x M square matrix

 

Ex)

Input

 m = one(3)

Output

 m =

   1   1   1

   1   1   1

   1   1   1

 

 

< Mathematical Operation >

 

Case 1 : m = matrix1 + matrix 2;

 

Ex)

Input

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

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

m = m1 + m2

Output

 m =

   10   10   10

   10   10   10

   10   10   10

 

 

Case 2 : m = matrix1 .* matrix 2; // element by element multiplication

 

Ex)

Input

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

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

m = m1 .* m2

Output

 m =

    9   16   21

   24   25   24

   21   16    9

 

 

Case 3 : m = matrix1 * matrix 2; // Inner Product

 

Ex)

Input

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

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

m = m1 * m2

Output

 m =

    30    24    18

    84    69    54

   138   114    90

 

 

Case 4 : m = matrix1 .^ n; // power of n for each element

 

Ex)

Input

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

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

m = m1 .^ 2;

Output

 m =

    1    4    9

   16   25   36

   49   64   81

 

 

Case 5 : m = matrix1 ^ n; // matrix power

 

Ex)

Input

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

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

m = m1 ^ 2;

Output

 m =

    30    36    42

    66    81    96

   102   126   150

 

 

Case 6 : tm = m'; // transpose matrix

 

Ex)

Input

m = [1 2 3; 6 5 4; 7 3 9];

tm=m'

Output

 tm =

     1   6   7

     2   5   3

     3   4   9

 

 

Case 7 : im = inv(m); // take the inverse matrix

 

Ex)

Input

m = [1 2 3; 6 5 4; 7 3 9];

im=inv(m)

Output

im = 

  -0.47143   0.12857   0.10000

   0.37143   0.17143  -0.20000

   0.24286  -0.15714   0.10000

 

 

Case 8 : im = det(m); // take the determinant of the matrix

 

Ex)

Input

m = [1 2 3; 6 5 4; 7 3 9];

dm=det(m)

Output

dm = 

  -70

 

 

< Indexing the elements - matrixname(rowindex,colindex) >

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(1,2)

Output

ans = 2

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(1,2) = 0

Output

m =

    1    0    3    4

    5    6    7    8

    9    10   11   12

 

 

< Indexing the elements - matrixname(:,colindex) >

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(:,2)

Output

ans =

    2

    6

    10

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(:,2) = 0

Output

m =

    1    0    3    4

    5    0    7    8

    9    0   11   12

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(:,2) = [15;16;17]

Output

m =

    1    15    3    4

    5    16    7    8

    9    17   11   12

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(:,2) = [15 16 17]'

Output

m =

    1    15    3    4

    5    16    7    8

    9    17   11   12

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(:,2) = [15 16 17].'

Output

m =

    1    15    3    4

    5    16    7    8

    9    17   11   12

 

 

< Indexing the elements - matrixname(rowindex,:) >

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(2,:)

Output

ans =

    5 6 7 8

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(2,:) = 0

Output

m =

    1    2    3    4

    0    0    0    0

    9   10   11   12

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12];

m(2,:) = [15 16 17 18]

Output

m =

    1    2    3    4

   15   16   17   18

    9   10   11   12

 

 

 

< Matrix Operators >

 

Case 1 : Eigenvalue and Eigenvector

 

Ex)

Input

A = [1.01 0;0 0.9];

[v,d]=eig(A)

Output

v =

 

   0   1

   1   0

 

d =

 

Diagonal Matrix

 

   0.90000         0

         0   1.01000

Input

A*v

Output

ans =

 

   0.00000   1.01000

   0.90000   0.00000

Input

v*d

Output

ans =

 

   0.00000   1.01000

   0.90000   0.00000

 

 

< Decomposition - LU Decomposition >

 

Ex)

Input

m = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]

Output

m = 

    1    2    3    4

    5    6    7    8

    9   10   11   12

   13   14   15   16

Input

[L,U] = lu(m)

Output

L = 

   0.07692   1.00000   0.00000   0.00000

   0.38462   0.66667  -0.63988   1.00000

   0.69231   0.33333   1.00000   0.00000

   1.00000   0.00000   0.00000   0.00000

 

U = 

   13.00000   14.00000   15.00000   16.00000

    0.00000    0.92308    1.84615    2.76923

    0.00000    0.00000   -0.00000   -0.00000

    0.00000    0.00000    0.00000    0.00000

Input

L * U

Output

ans = 

    1    2    3    4

    5    6    7    8

    9   10   11   12

   13   14   15   16

 

 

< Rearranging Elements - circshift() >

 

Case 1 : m = circshift(m1,N) // Where N is a positive Number. This shift rows

 

Ex)

Input

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

m = circshift(m1,1)

Output

 m =

    7   8   9

    1   2   3

    4   5   6

 

 

Case 2 : m = circshift(m1,N) // Where N is a Negative Number. This shift rows

 

Ex)

Input

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

m = circshift(m1,-1)

Output

 m =

   4   5   6

   7   8   9

   1   2   3

 

 

Case 3 : m = circshift(m1,[0 N]) // Where N is a Positive Number. This shift cols

 

Ex)

Input

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

m = circshift(m1,[0 1])

Output

 m =

   3   1   2

   6   4   5

   9   7   8

 

 

Case 3 : m = circshift(m1,[0 N]) // Where N is a Negative Number. This shift cols

 

Ex)

Input

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

m = circshift(m1,[0 1])

Output

 m =

   2   3   1

   5   6   4

   8   9   7

 

 

< Rearranging Eelemetns - resize() >

 

Case 1 : m = resize(m1,M,N,..)

// Resize Matrix m1 to be an M x N x .. matrix, where M, N is smaller than the original matrix

 

Ex)

Input

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

m = resize(m1,3,2)

Output

 m =

   1   2

   4   5

   7   8

 

 

Case 2 : m = resize(m1,M,N,..)

// Resize Matrix m1 to be an M x N x .. matrix, where M or N is smaller than the original matrix

 

Ex)

Input

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

m = resize(m1,4,5)

Output

 m =

   1   2   3   0   0

   4   5   6   0   0

   7   8   9   0   0

   0   0   0   0   0

 

 

Case 3 : m = resize(m1,M) // Resize Matrix m1 to be an M x M  matrix

 

Ex)

Input

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

m = resize(m1,2)

Output

 m =

   1   2   

   4   5