MATLAB supports three logical operators.

Matrix

A portion of a matrix can be extracted and stored in a smaller
matrix by specifying the names of both matrices and the rows
and columns to extract. The syntax is:

sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;

where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix.

*Example

>> X = [1,2,3;4,5,6;7,8,9]
X =
1 2 3
4 5 6
7 8 9
>> X22 = X(1:2 , 2:3)
X22 =
2 3
5 6
>> X13 = X(3,1:3)
X13 =
7 8 9
>> X21 = X(1:2,1)
X21 =
1
4

*Adding two matrices

>> xsy = x + y
xsy =
7 9
11 13
>> z = [1,0.3]
z =
1 0.3
>> xsz = x + z
??? Error using => plus  Matrix dimensions must agree

*Matrix multiplication

>> a = [1,2;3,4]; (2x2)
>> b = [1,1]; (1x2)
>> c = b*a
c =
4 6
>> c = a*b
??? Error using ==> mtimes Inner matrix dimensions must agree.

*Element wise multiplication

>> a = [1,2;3,4];
>> b = [1,½;1/3,¼];
>> c = a.*b
c =
1 1
1 1
>> a = [1,2;1,3];
>> b = [2,2;2,1];  

Element wise division

>> c = a./b
c =
0.5 1
0.5 3

Element wise multiplication

>> c = a.*b
c =
2 4
2 3

Element wise power operation

>> c = a.^2
c =
1 4
1 9
>> c = a.^b
c =
1 4
1 3

2D Plotting

Method 1:

>> t1 = linspace(0,1,1000);
>> t2 = linspace(1,6,1000);
>> y1 = t1;
>> y2 = 1./ t2;
>> t = [t1,t2];
>> y = [y1,y2];
>> figure
>> plot(t,y);
>> xlabel ‘t values', ylabel ‘y values';

2dplot

Method 2:

>> t = linspace(0,6,1000);
>> y = zeros(1,1000);
>> y(t()<=1) = t(t()<=1);
>> y(t()>1) = 1./ t(t()>1);
>> figure
>> plot(t,y);
>> xlabel‘t values';
>> ylabel‘y values';

2dplot

Subplots

Read/write from a text file

MATLAB has five flow control statements

‘if' statement

‘switch' statement

switch Switch among several cases based on expression

‘for' loop

‘while' loop

‘break' statement

Example 1

Example 2

Solution

Where to get help?