0% found this document useful (0 votes)
9 views

MATLAB_basics_PMSS

The document provides an overview of MATLAB, a high-performance language for technical computing, emphasizing its capabilities in computation, visualization, and programming. It covers fundamental concepts such as the MATLAB environment, calculations, matrices, loops, and operators, illustrating how to manipulate data using MATLAB's syntax. The document also highlights the importance of matrices in MATLAB, explaining how to create, index, and perform operations on them.

Uploaded by

kashyapishu1611
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

MATLAB_basics_PMSS

The document provides an overview of MATLAB, a high-performance language for technical computing, emphasizing its capabilities in computation, visualization, and programming. It covers fundamental concepts such as the MATLAB environment, calculations, matrices, loops, and operators, illustrating how to manipulate data using MATLAB's syntax. The document also highlights the importance of matrices in MATLAB, explaining how to create, index, and perform operations on them.

Uploaded by

kashyapishu1611
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

BASICS OF MATLAB

GIRDHAR GOPAL
Research Scholar
ECE department, MNIT JAIPUR
What Is MATLAB?

MATLAB (MATrix LABoratory)

• high-performance language for technical computing


• computation, visualization, and programming in an easy-to-use environment
Workspace/Variable
Inspector

MATLAB USER Command Window

ENVIRONMENT
Command History
CALCULATIONS ON
THE COMMAND LINE
>>
>> -5/(4.8+5.32)^2
-5/(4.8+5.32)^2 >>
>> aa == 2;
2; Semicolon suppresses
ans
ans == >>
>> AA == 5;
5; screen output
-0.048821
-0.048821 >>
>> a^A
a^A
ans Variables are case
>> ans ==
>> (3+4i)*(3-4i)
(3+4i)*(3-4i) sensitive
ans 32
ans == 32
25
25 >>
>> xx == 5/2*pi;
5/2*pi;
>>
>> yy == sin(x)
sin(x) Results assigned to
>>
>> cos(pi/2)
cos(pi/2) yy == “ans” if name not given
ans
ans == 11
6.1232e-017
6.1232e-017
>>
>> zz == asin(y)
asin(y) Use parentheses ( )
>> zz ==
>> exp(acos(0.3))
exp(acos(0.3)) for function inputs
ans
ans == 1.5708
1.5708
3.547
3.547

Assigning Variables
MATLAB as a calculator
What does Matlab code look like?

Another simple example:

t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
What does Matlab code look like?

Another simple example:


Remember:
t = 0:pi/100:2*pi;
y = sin(t); EVERYTHING IN MATLAB IS A
plot(t,y)
MATRIX !

creates 1 x 200 Matrix

Argument and result: 1 x 200 Matrix


Matrices
Matrices

•Rows and columns are always numbered starting at 1

•Matlab matrices are of various types to hold different


kinds of data (usually floats or integers)

• A single number is really a 1 x 1 matrix in Matlab!

• Matlab variables are not given a type, and do not need


to be declared

• Any matrix can be assigned to any variable


Matrices

Building matrices with [ ]:

A = [2 7 4] 2 7 4

A = [2; 7; 4] 2
7
4

A = [2 7 4; 3 8 9] 2 7 4
3 8 9

B=[AA] ?
Matrices

Building matrices with [ ]:

A = [2 7 4] 2 7 4

A = [2; 7; 4] 2
7
4

A = [2 7 4; 3 8 9] 2 7 4
3 8 9

B=[AA] 2 7 4 2 7 4
3 8 9 3 8 9
Matrices
Matrices

Some operators must be handled with care:

A = [1 2 ; 4 5]

B=A*A prints 9 12
24 33

B = A .* A prints 1 4
16 25

Element by element multiplication


Submatrices

A matrix can be indexed using another matrix, to


produce a subset of its elements:

a = [100 200 300 400 500 600 700] b = [3 5 6]

c = a(b):

300 500 600


Submatrices

To get a subsection of a matrix, we can produce the


index matrix with the colon operator:

a(2:5)
prints
ans = 200 300 400 500

•This works in 2-D as well, e.g. c(2:3, 1:2) produces a


2 x 2 submatrix.

•The rows and columns of the submatrix are


renumbered.
loops

‘for’ loops in MATLAB iterate over matrix elements:

b=0
for i = [ 3 9 17]
b = b + i;
end

Result: 29

Note:
The MATLAB way to write that program would have been:
b = sum([ 3 9 17]);

Avoid loops if possible !


loops

The typical ‘for’ loop looks like:

for i = 1:6

end

Which is the same as:

for i = [1 2 3 4 5 6]

end
ENTERING NUMERIC ARRAYS
Row separator: >>
>> a=[1
a=[1 2;3
2;3 4]
4]
Semicolon (;) or aa == Use square
newline 11 22 brackets [ ]
Column separator: 33 44
space or comma (,)
>>
>> bb == [2:-0.5:0]
[2:-0.5:0]
bb ==
Creating sequences 22 1.5 11 0.5 00
1.5 0.5
using the colon
operator (:) >>
>> cc == rand(2,4)
rand(2,4)
cc ==
Utility function for 0.9501
0.9501 0.6068
0.6068 0.8913
0.8913 0.4565
0.4565
creating matrices. 0.2311
0.2311 0.4860
0.4860 0.7621
0.7621 0.0185
0.0185

Matrices must
be rectangular. (Undefined
elements set to zero)
ENTERING NUMERIC ARRAYS
(CONTINUED)
Using other MATLAB >>
>> ww == [-2.8,
[-2.8, sqrt(-7),
sqrt(-7), (3+5+6)*3/4]
(3+5+6)*3/4]
expressions ww ==
-2.8
-2.8 00 ++ 2.6458i
2.6458i 10.5
10.5

Matrix element >>


>> m(3,2)
m(3,2) == 3.5
3.5
mm ==
assignment 00 00
00 00
00 3.5
3.5

Adding to an existing >>


>> w(2,5)
w(2,5) == 23
23
array ww ==
-2.8
-2.8 00 ++ 2.6458i
2.6458i 10.5
10.5 00 00
00 00 00 00 23
23

Note: MATLAB deals with


Imaginary numbers…
Rectangular Matrix:

Scalar: 1-by-1 array

Vector: m-by-1 array


Columns 1-by-n array
(n)
1 2 3 4 5 Matrix: m-by-n array
A= 4
1
10
6
1
11
6
16
2
21
A (2,4)
1
2
2 8 1.2 7 9 12
4 17
25 22

Rows (m) 3 7.2 3 5 8


7 13
1 18
11 23 A (17)
4 0 4
0.5 9 4 14
5 19
56 24
5 5 10 15 20 25
23 83 13 0 10

INDEXING INTO A MATRIX IN MATLAB


ARRAY SUBSCRIPTING /
INDEXING
1 2 3 4 5
A= 4
1
10
6
1
11
6
16
2
21

2 8 2
1.2 7 9 12
4 17
25 22
A(1:5,5) A(1:end,end)
3 7.2 3
5 8
7 13
1 18
11 23 A(:,5) A(:,end)
A(21:25) A(21:end)’
4 0 4
0.5 9 4 14
5 19
56 24
A(3,1)
A(3) 5 5 10 15 20 25
23 83 13 0 10
A(4:5,2:3)
A([9 14;10 15])
• Use () parentheses to specify index
• colon operator (:) specifies range / ALL
• [ ] to create matrix of index subscripts
• 'end' specifies maximum index value
THE COLON OPERATOR
>> N = 5:10:35
N =
 5 15operator
Colon 25 35 occurs in several forms

>> P To= indicate
[1:3;a range (as above)
30:-10:10]
 To indicate a range with non-unit increment
P =
1 2 3
30 20 10
To extract ALL the elements of an array (extracts
everything to a single column vector)

>> A = [1:3; >> A(:)


10:10:30; ans =
1
100:100:300]
10
A = 100
1 2 3 2
20
10 20 30
200
100 200 300 3
30
300
Use [ ] to combine >>
>> a=[1
a=[1 2;3
2;3 4]
4]
existing arrays as aa == Use square
matrix “elements” 11 22 brackets [ ]
33 44
Row separator: >>
>> cat_a=[a,
cat_a=[a, 2*a;
2*a; 3*a,
3*a, 4*a;
4*a; 5*a,
5*a, 6*a]
6*a]
semicolon (;) cat_a
cat_a ==
11 22 22 44
33 44 66 88
Column separator: 33 66 44 88
4*a
space / comma (,) 99 12
12 12
12 16
16
55 10
10 66 12
12
15
15 20
20 18
18 24
24

NUMERICAL ARRAY
CONCATENATION [ ]
MATRIX AND ARRAY OPERATORS
Matrix Operators Array operators

() parentheses Common Matrix Functions


inv matrix inverse
' complex conjugate .' array transpose
transpose det determinant
^ power .^ array power
rank matrix rank
* multiplication .* array mult. eig eigenvectors and
eigenvalues
/ division ./ array division svd singular value dec.

\ left division norm matrix / vector norm

+ addition

- subtraction

>> help ops >> help matfun


 1 & 2D arrays are treated as formal matrices
 Matrix algebra works by default:

>> a=[1 2]; 1x2 row oriented array (vector)


>> b=[3 (Trailing semicolon suppresses
4]; display of output)
2x1 column oriented array
>> a*b
ans = Result of matrix multiplication
11 depends on order of terms
(non-cummutative)
>> b*a
ans =
3 6
4 8
 Element-by-element (array) operation is forced by preceding
operator with a period ‘.’

>> a=[1 2];


>> b=[3
4];
>> c=[3 4];

>> a.*b
??? Error using
Matrix dimensions must agree.
Size and shape must match
>> a.*c

ans =
3 8
MATRIX CALCULATION-
SCALAR EXPANSION
>>
>> w=[1
w=[1 2;3
2;3 4]
4] ++ 55
ww ==
66 77
88 99

>>
>> w=[1
w=[1 2;3
2;3 4]
4] ++ 55

11 22
== ++ 55
33 44
Scalar expansion
11 22 55 55
== ++
33 44 55 55

66 77
==
88 99
Inner dimensions must be equal.
Dimension of resulting matrix = outermost
dimensions of multiplied matrices.
Resulting elements = dot product of the rows
of the 1st matrix with the columns of the 2nd
matrix.
MATRIX
MULTIPLICATION
>>
>> aa == [1
[1 22 3;4
3;4 55 6];
6]; [2x3]
>>
>> bb == [3,1;2,4;-1,2];
[3,1;2,4;-1,2]; [3x2]
>>
>> cc == a*b
a*b [2x3]*[3x2] [2x2]
cc ==
44 15
15
16
16 36
36 a(2nd row).b(2nd column)
 Matrices must have the same dimensions (size and shape)
 Dimensions of resulting matrix = dimensions of multiplied
matrices
 Resulting elements = product of corresponding elements
from the original matrices

>>
>> aa == [1
[1 22 33 4;
4; 55 66 77 8];
8];
>>
>> bb == [1:4;
[1:4; 1:4];
1:4];
>>
>> cc == a.*b
a.*b
cc ==
11 44 99 16
16
55 12
12 21
21 32
32

 Same rules apply for other array operations

ARRAY (ELEMENT-BY-
ELEMENT)
MULTIPLICATION
>> A = [1:3;4:6;7:9]
A =
1 2 3
Many common functions
4 5 6
operate on columns by
7 8 9 default
Mean of each column in A
>> mean(A)
ans =
4 5 6

>> sum(A)
ans =
12 15 18 Mean of all elements in A

>> mean(A(:))
ans =
5
>> clear clear all workspace
>> clear VARNAME clear named variable
>> clear all clear everything
(see help clear)
>> close all close all figures
>> clc clears command
window display only

CLEARING UP
Any inbuilt MATLAB function
documentation can be found out by
typing:
>> help function_name
Or
>> lookfor function_name

You might also like