Professional Documents
Culture Documents
Introduction to MATLAB
R Prasanth Kumar
This one credit lab course runs for two months and deals with how to use MATLAB
for scientic computation.
>> x = 5
>> y = 2
>> z = x+y
3. Repeat with semicolon at the end of rst and
second lines.
4. Try this:
We have already seen how to dene scalar variables in the previous slide.
>> x = [1 0 -8 6 7]
>> y = [1 0 -8 6 7]
Single quote at the end of a vector or matrix means transpose.
Dening a matrix:
>> z = [1 2 3;4 5 6]
a 2 x 3 matrix. Semicolon separates the rows of matrix.
>> size(z)
gives a two element row vector as output with rows of z as the rst element, and
columns of z as the second element. To get number of rows or columns separately,
use size(z,1) or size(z,2)
Identity Matrix
>> x = 1:10
>> y = 1:2:10
>> z = rand(3,4)
>> x = 0:0.5:3
>> y = 5*x.^3-50
>> A+2
>> A-3
Element-wise multiplication or
division
>> A.^2
Paranthesis ( )
Transpose, power (with/without .)
Unary plus, unary minus
Multiplication and division
Addition and subtraction
>> mean(A,1)
>> mean(A,2)
>> diag(A)
>> x = 1:10
>> sum(x)
>>
>>
>>
>>
det(A)
rank(A)
eig(A)
[V,D] = eig(A)
V contains eigenvectors as
columns, and D contains
eigenvalues as diagonal elements
>> sum(A)
>> sum(A,2)
>>
>>
>>
>>
>>
>>
>> A(1,:)
>> A(:,2)
Colon indicates all.
>> A(2:3,1:2)
x = [5 6 7 8 9];
y = [4 5 1 2 3];
x(1,4)
x(4)
y(3,1)
y(3)
or
>> x(1:2:size(x,2))
or
Concatenation of Matrices
>> B = [x z]
Number of rows in x and z should be same.
>> x = [3 0 5 -2 8];
>> min(x)
>> max(x)
>>
>>
>>
>>
>> min(z,[],2)
>> max(z,[],2)
>> x(find(x>4))
Miscellaneous Commands
>> clear x y
>> clc
x = [-3 2 0 7 -5 10];
find(x>4)
find(x<2)
find(x==7)