BASIC MATLAB TUTORIAL : PART 2

In this blog i will try to show you few in build matrix functions present in MATLAB. I will show you how to distribute a range of values in particular step size , see a particular row or column and see the no. of elements present in a matrix/statements.

The statements written in “GREEN” colour are ignored by the compilers and they are present in a code to help the reader/programmer understand it better.

The codes are written in the highlighted section which we type in the command window.

1.There are few standard matrix functions available in MATLAB. In my later post i will show you how to make your own function in MATLAB and use it as you wish. Lets see few standard functions :

(a) “zero” function creates a m*n matrix having all elements as “0” , where m=no. of rows and n=no. of columns.

>> z = zeros(3,3)

z =

0   0   0
0   0   0
0   0   0

(b)”ones” function creates a m*n matrix having all elements as “1” , where m=no. of rows and n=no. of columns.

>> o = ones(3,3)

o =

1   1   1
1   1   1
1   1   1

(c)”eye” function creates a m*n matrix having all diagonal elements as “1” and rest as zero , where m=no. of rows and n=no. of columns. This type of matrix is called identity matrix.

>> e = eye(3,3)

e =

1   0   0
0   1   0
0   0   1

(d)”rand” function creates a m*n matrix having all  elements created randomly between 0 and 1, where m=no. of rows and n=no. of columns. This type of matrix is called random matrix. Every time we run the code we will get different matrix elements but they will all lie between 0 and 1 only.

>> r=rand(3,3)

r =

0.7922 0.0357 0.6787
0.9595 0.8491 0.7577
0.6557 0.9340 0.7431

To get the values in the units of 10 we can use the following tweak: here i will be taking the help of another build in function called “floor”. Suppose my value is 3.4. So, Floor(3.4) will be 3. Similarly floor(3.6) will also be 3.

You can also use “ceil”. Example ceil(4.2) will be 5.

>> r = floor(10*rand(3,3))

r =

3   7   0
6   0   0
1   2   8

I multiplied my random matrix by 10 and then took the floor. So now my values will lie in range of 0 and 10.

To get the range between 0 and 100 , multiply by 100.

>> r = floor(100*rand(3,3))

r =

69    3    76
31   43    79
95   38    18

2.There is a way to see a particular column or particular row of a matrix. “: ” symbol represents the entire array of that row or column.

NOTE: a(m,n) represents a matrix “a” having “m” rows and “n” columns.

So,

a(2,3) will mean 2nd rows 3rd columns element.

a(2,:) will mean 2nd rows entire columns elements.

See the example and try yourself to understand better.

>> a=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20;21 22 23 24 25]

a =

1    2    3    4    5
6    7    8    9   10
11  12   13   14   15
16  17   18   19   20
21  22   23   24   25

>> a(1,:)  %to see the 1st rows entire column

ans =

1   2   3   4   5

>> a(:,1)  %to see the 1st columns entire row

ans =

1
6
11
16
21

>> a(3,:)  %to see the 3rd rows entire column

ans =

11   12   13   14   15

>> a(:,3)  %to see the 3rd columns entire row

ans =

3
8
13
18
23

To see a particular element of a matrix we can use the same procedure. Just replace ” : ” by the value of the row or column.

a =

1     2     3     4     5
6     7     8     9    10
11   12    13    14    15
16   17    18    19    20
21   22    23    24    25


>> a(2,3)%to see the 2nd row 3rd column

ans =

8

>> a(4,5)%to see the 4th row 5th column

ans =

20

3.How to distribute a range in a particular step size ? Just see the code and the output:

>> distribute_a_range=1:0.5:4 %DISTRIBUTE A RANGE BETWEEN 1 TO 4 IN STEP SIZE 0.5

distribute_a_range =

1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000
>> distribute_a_range=1:1:4 %DISTRIBUTE A RANGE BETWEEN 1 TO 4 IN STEP SIZE 1

distribute_a_range =

1    2    3    4

So, the code is formated in this manner:

Name=Initial Value : Step size : Final value.

4. There is a way to see the size of a matrix: The function for it is “size( )”. As input argument to the function we give the name of the matrix/variable. See the code and example below:

>> r = rand(3,4) %generated a 3*4 matrix

r =

0.8407    0.2435    0.1966    0.4733
0.2543    0.9293    0.2511    0.3517
0.8143    0.3500    0.6160    0.8308

>> size(r)% input argument to the function is the name of the matrix

ans =

3    4

where 3 is the no. of row and 4 is the no. of column.

We can use it to see the no. of characters in a statement. See below:

>> a='see my website regularly'

a =

see my website regularly

>> size(a)

ans =

1    24

Here 24 represents the no. of letters/characters used. Note a point that, “space bar” are also regarded as character. Count yourself and see.

Keep visiting the website.

PART 3 IS PRESENT HERE:  http://ilearn2share.com/2017/02/19/basic-matlab-tutorial-part-3/

BASIC MATLAB TUTORIAL : PART 1

Matlab is a robust software. We can perform a lot of numerical operations on it. This blog series is just for the beginners. Its an introductory series so only the basic operations will be considered.

NOTE: Things written after ” >> “ this symbols are the the codes written in the MATLAB command window & the things written after ” = “ are the output of the MATLAB codes.

  1. Can MATLAB be used as a calculator ?The answer to this question is Yes, but it will be a very expensive calculator. We can perform all the mathematical operations in it directly.>> (1+2-3*(6+5))/2
    ans =
    -15>> 1-2+3*(4/5)
    ans =
    1.4000

    >> sin(30)^2 + cos(30)^2

    ans =

    1

    >> tan(45)+cot(45)

    ans =

    2.2371

    i and j represents the same complex parameter:

    >> (1+2i)+(3+4i)

    ans =

    4.0000 + 6.0000i

    >> (1+2j)+(3+4j)

    ans =

    4.0000 + 6.0000i

  2. Difference between “format long” and “format short”: To get upto 15 decimal place of a value we will use “format long” before the operation.>> format long
    >> 22/7
    ans =
    3.142857142857143″format short” is used to get upto 4 decimal place.

    >> format short
    >> 22/7
    ans =
    3.1429

  3. How to write matrix in the MATLAB ? Matrix is the basic building block of the entire operation of MATLAB so this point is very important . See the difference : the matrix are written in [ row*column ] format
    (a) Its a 1*1 matrix having the element 1234
    >> A=[1234]
    A =
    1234(b) Its a 1*4 row matrix having the 4 element as  1 , 2 , 3, 4
    >> B=[1 2 3 4]
    B =

    1     2     3     4

    (c) Its a 4*1 column matrix having the 4 element as  1 , 2 , 3, 4

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

    C =

    1
    2
    3
    4

    (d) Its a 2*2 Square matrix having the 4 element as  1 , 2 , 3, 4. The ” ; ” symbol generates the row and the ” ” space key generates the column.

    >> X=[1 2;3 4]

    X =

    1     2
    3    4

    (e) a 2*3 matrix

    >> y=[1 2 3;4 5 6]

    y =

    1    2    3
    4   5    6

  4. How to create a constant and change the value of the matrix: Let us assume the symbol ” k ” is a variable representing constant. I want to modify my Matrix x as shown below:NOTE: The text written in GREEN COLOUR are the comment statements. They are Not compiled or run in the MATLAB operations. They are used just to make the user understand the program properly. Its a very good practice to write comments as often as possible..>> x=[1 2;3 4] % My matrix
    x =
    1     2
    3    4>> k=10 % My constant value

    k =

    10

    >> x_modify_1=k*X % Matrix multiplied by k

    x_modify_1 =

    10     20
    30    40

    >> x_modify_2=X./k % Matrix divided by k

    x_modify_2 =

    0.1000     0.2000
    0.3000    0.4000

    Note a point that i have use ” ./ ” for division and not the usual ” / ” symbol because matrix division is an element to element operation and must be performed for all the elements.

  5. How to find transpose of a Matrix ?>> a=[11    12    13;14    15    16;17    18    19]a =
    11     12     13
    14    15    16
    17    18    19

    >> a_transpose=a’         %” a_transpose ” is just a variable name given to the transpose

    a_transpose =

    11     14     17
    12    15    18
    13    16    19

    ” ‘ ” this symbol will perform the transpose of the matrix.There is an alternate way of performing transpose and that is by using the standard build in function called ” transpose( )”.

    >> a_transpose_alternate=transpose(a)  %alternate way of transposing

    a_transpose_alternate =

    11     14     17
    12    15    18
    13    16    19

  6. How to display a statement/value in MATLAB? For this we will take the help of the in-build function present in MATLAB which is ” disp(‘ your statement‘) ” . Lets see an example to display a statement:>> disp(‘Hi.. My Name is MOUSAM CHOUDHURY’)
    Hi.. My Name is MOUSAM CHOUDHURYWe can also display values with this function:

    >> a=[12 13]

    a =12     13

    >> disp(a)
    12     13

    >> sum=7+8;    %  ” ; “ symbol after a command prevents the result from displaying

    >> disp(sum)
    15

    another example:

    >> x=’Thank you for watching my website:http://ilearn2share.com/’ ;
    >> disp(x)
    Thank you for watching my website:http://ilearn2share.com/

    Part 2 of this Basic MATLAB tutorial series will be uploaded soon.