Matlab/Octave

 

 

 

 

Vector

 

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 !!!

 

 

Vector (One Dimmensional Array)

 

 

Vector (One Dimmensional Array)

 

< Creating a vector >

 

Method 1 :  v = [ value value value value ...]

 

Ex)

Input

v = [ 1 2 4 7 2 1]

Output

v =

   1   2   4   7   2   1

 

 

Method 2 :  v = start:step:end

 

Ex)

Input

v=1:0.2:2

Output

v =

    1.0000    1.2000    1.4000    1.6000    1.8000    2.0000

 

 

< Creating a vector with linspace >

 

Method 1 :  v = linspace(start_value,end_value,num_of_data)

 

Ex)

Input

v = linspace(1,4,10)

Output

v =

   1.0000   1.3333   1.6667   2.0000   2.3333   2.6667   3.0000   3.3333   3.6667   4.0000

 

 

< Mathematical Operation >

 

Case 1 :  v = vector1 + vector2

 

Ex)

Input

v1=[1 2 3 4];

v2=[5 6 7 8];

v=v1 + v2

Output

v =

    6    8   10   12

 

 

Case 2 :  v = vector1 - vector2

 

Ex)

Input

v1=[1 2 3 4];

v2=[5 6 7 8];

v=v1 - v2

Output

v =

    -4    -4   -4   -4

 

 

Case 3 :  v = vector1 . vector2 (Inner Product)

 

Ex)

Input

v1=[1 2 3 4];

v2=[5 6 7 8];

v=v1 * v2' // Note : I put the transpose operator (') here. Try v = v1*v2 and see what happen.

Output

v =

    70

 

 

Case 4 :  v = vector1 x vector2 (multiplication of each elements)

 

Ex)

Input

v1=[1 2 3 4];

v2=[5 6 7 8];

v=v1 .* v2 // Note : I used .*, not * . Try v = v1*v2 and see what happen.

Output

v =

    5   12   21   32

 

 

Case 5 :  v = vector1 / vector2 (division of each elements)

 

Ex)

Input

v1=[1 2 3 4];

v2=[5 6 7 8];

v=v1 ./ v2 // Note : I used ./, not / 

Output

v =

     0.20000   0.33333   0.42857   0.50000

 

 

Case 6 :  v = scalar + vector2

 

Ex)

Input

v1 =[1 2 3 4];

s = 2;

v= s + v1 // Note : In this case, v = s .+ v1 will give the same result

Output

v =

     3   4   5   6

 

 

Case 7 :  v = scalar x vector2

 

Ex)

Input

v1 =[1 2 3 4];

s = 2;

v= s .* v1  // Note : In this case, v = s * v1 will give the same result

Output

v =

     2   4   6   8

 

 

< Applying functions >

 

Case 1 :  v = function(vector)

 

Ex)

Input

v1 =[1 2 3 4];

v= sin(v1) 

Output

v =

     0.84147   0.90930   0.14112  -0.75680

 

 

Ex)

Input

v1 =[1 2 3 4];

v= v1.^2 + 2 .* v1 + cos(v1)

Output

v =

     3.5403    7.5839   14.0100   23.3464

 

 

< Referencing the elements >

 

Case 1 :  v = vector (index)

 

Ex)

Input

v1 =[1 2 3 4];

v= v1(3)

Output

v =

     3

 

 

Case 2 :  v = vector ([index range])

 

Ex)

Input

v1 =[1 2 3 4];

v= v1([1:3])

Output

v =

     1 2 3

 

 

Case 3 :  v = vector ([index list])

 

Ex)

Input

v1 =[1 2 3 4];

v= v1([4 2 3])

Output

v =

     4 2 3

 

 

Case 4 :  v = vector (end)

 

Ex)

Input

v1 =[1 2 3 4];

v= v1(end)

Output

v =

     4

 

 

< Concatenating Vectors >

 

Case 1 :  v = [v1 v2] // All the logic is same

 

Ex)

Input

v1 =[1 2 3 4];

v2 =[5 6 7 8];

v= [v1 v2]

Output

v =

      1   2   3   4   5   6   7   8

 

 

Ex)

Input

v1 =[1 2 3 4];

v2 =[5 6 7 8];

v= [v1 v2 -v1]

Output

v =

      1   2   3   4   5   6   7   8  -1  -2  -3  -4

 

 

Ex)

Input

v=[];   // create a variable named 'v' and initialize it with an empty array

for i = 1:10

      v=[v i];

end

v

Output

v =

      1   2   3   4   5   6   7   8   9  10

 

 

< Removing/Deleing Elements >

 

Case 1 : Removing/Deleting one element from a Vector

 

Ex)

Input

v = [1 2 3 4 5];

v(3) = []

Output

v =

         1  2  4  5

 

 

Case 2 : Removing/Deleting multiple elements from a Vector

 

Ex)

Input

v = [1 2 3 4 5];

v([2 5]) = []

Output

v =

         1  3  4

 

 

 

< Rearranging Elements - shift() >

 

Case 1 : v = shift(vector,N) // Where N is a Positive Number

 

Ex)

Input

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

v = shift(v1,3)

Output

v =

         8    9   10    1    2    3    4    5    6    7

 

 

Case 2 : v = shift(vector,N) // Where N is a Negative Number

 

Ex)

Input

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

v = shift(v1,-3)

Output

v =

         4    5    6    7    8    9   10    1    2    3

 

 

< Getting the size of a vector : size() >

 

Case 1 : v = size(v) // returns number of cols and number of rows of v

 

Ex)

Input

t = 0:0.1:10;

size(t)

Output

ans =

     1   101

 

 

Case 2 : v = size(v,1) // returns the number of rows of v

 

Ex)

Input

t = 0:0.1:10;

size(t,1)

Output

ans =

     1  

 

 

Case 3 : v = size(v,2) // returns the number of rows of v

 

Ex)

Input

t = 0:0.1:10;

size(t,2)

Output

ans =

     101

 

 

< Getting the size of a vector : length() >

 

Case 1 : v = length(v) // returns number of elements of v

 

Ex)

Input

t = 0:0.1:10;

length(t)

Output

ans =

     101

 

 

 

< Converting a Vector into a Matrix (Converting one dimmensional array into multi dimensional array) >

 

Case 1 : v = reshape(vector,rows,cols)

 

Ex)

Input

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

v = reshape(v1,2,5)  // Note : Number of matrix elements and Vector elements should be same

Output

v =

         1   2   3   4   5

         6   7   8   9  10

 

 

< norm >

 

Case 1 : v_n = norm(realvector);

 

Ex)

Input

r = [1 2 3 2 5];

r_n = norm(r);

Output

r_n =

      6.5574

 

norm() performs following procedure.

 

Input

r = [1 2 3 2 5];

r_n = sqrt(sum(abs(r).^2));

Output

r_n =

      6.5574

 

 

Case 2 : v_n = norm(complexvector);

 

Ex)

Input

c = [1+2*j  2+5*j  3+2*j  4-2*j];

c_n = norm(c);

Output

c_n =

      8.1854

 

norm() performs following procedure.

 

Input

c = [1 2 3 2 5];

c_n = sqrt(sum(abs(c).^2));

Output

c_n =

      8.1854