P1 : MATLAB Basics

To declare a variable.
a=10

a =

    10

Note : If you will put ; (semi colons) the you will not output of that variable  or command. As seen blow :
>> a=10;
>> b=30;





Addition of two variable.
>> c=a+b

c =

    40

Subtraction of two variable.
>> c=a-b

c =

   -20

Multiplication of two numbers.
>> c=-1*c

c =

    20

Similarly you can do divide operation using / symbol between two variables.

Command for writing 1 to 10
>> a=1:10

a =

     1     2     3     4     5     6     7     8     9    10
Note : By putting : colons you can  start from a number and count to another number. Here I have use 1 as starting and 10 as ending.


 Command for writing or storing vector as counting and giving same gap between the number s.
>> a=1:2:10

a =

     1     3     5     7     9
Note : In this example I have start from 1 and storing after a gap of 2 till 10.

Similarly you can write down table of any nubmer.
>> a=3:3:30

a =

     3     6     9    12    15    18    21    24    27    30

Matrix
Command to create matrix 

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

A =

     1     2     3
     5     6     7
     8     9    10 

Note : Semi-colons are to enter elements in next row.

Command to create vector 
A=[1 2 3 4 5]

A =

     1     2     3     4     5
or 
A=[1:5]

A =

     1     2     3     4     5
Both are same things.

Random elements matrix command
Mat = rand(3)

Mat =

    0.0462    0.6948    0.0344
    0.0971    0.3171    0.4387
    0.8235    0.9502    0.3816
Note 3 is to create square 3X3 matrix.

Similarly you can create random elements matrix but not square matrix. 
So command is here.
Mat = rand(3,4)

Mat =

    0.7655    0.4898    0.7094    0.6797
    0.7952    0.4456    0.7547    0.6551
    0.1869    0.6463    0.2760    0.1626
  Note (3,4) is for 3x4 matrix.

Mathematical Operations on Matrix

Sum command

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

A =

     1     2     3
     5     6     7
     8     9    10
Mat = rand(3)

Mat =

    0.0462    0.6948    0.0344
    0.0971    0.3171    0.4387
    0.8235    0.9502    0.3816

>> C=Mat+ A

C =

    1.0462    2.6948    3.0344
    5.0971    6.3171    7.4387
    8.8235    9.9502   10.3816
Note: Matrix should be of same dimensions. Here both matrix are of 3X3.

Subtraction
>>C=Mat - A

C =

   -0.9538   -1.3052   -2.9656
   -4.9029   -5.6829   -6.5613
   -7.1765   -8.0498   -9.6184

Multiplication
>>C=Mat * A

C =

    3.7959    4.5713    5.3468
    5.1926    6.0456    6.8985
    8.6270   10.7823   12.9375

Co-responding elements Multiplication
>> C=Mat .* A

C =

    0.0462    1.3897    0.1033
    0.4857    1.9026    3.0712
    6.5877    8.5520    3.8156
 Apply . before *.

Co-responding elements Division 
 >> C=Mat ./ A

C =

    0.0462    0.3474    0.0115
    0.0194    0.0528    0.0627
    0.1029    0.1056    0.0382

Extracting elements from matrix
 Declare Matrix.
>> A=[1 2 3; 5 6 7; 8 9 10]

A =

     1     2     3
     5     6     7
     8     9    10
Extracting location 2,2 form A matrix.
>> B(2,2)

ans =

     6
Extracting elements from 2nd row and 2nd and 3rd cols.
>> B(2,2:3)

ans =

     6     9
Extracting elements from 2nd row and 2nd to end cols
>> B(2,2:end)

ans =

     6     9
Extracting 2nd complete Row.
>> B(2,:)

ans =

     2     6     9
Extracting 2nd and 3rd Row and 2nd Col
>> B(2:3,2)

ans =

     6
     7
Extracting 2nd to end Rows and 2nd Col
>> B(2:end,2)

ans =

     6
     7

>> B(1:3,2)

ans =

     5
     6
     7

Determination Command :

>>det(A)

ans =

     0

Inverse Matrix Command

>>inv(A)
Warning: Matrix is singular to working precision.

ans =

   Inf   Inf   Inf
   Inf   Inf   Inf
   Inf   Inf   Inf

Note if determination of any matrix is zero than ans of inv(A) is Inf (infinity).

Transpose a matrix :
>> B=A'

B =

     1     5     8
     2     6     9
     3     7    10

or

>> transpose(A)

ans =

     1     5     8
     2     6     9
     3     7    10

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons