Académique Documents
Professionnel Documents
Culture Documents
Matlab Matrices
Matlab treats all variables as matrices.
Vectors are special forms of matrices and contain only one row OR one
column.
Scalars are matrices with only one row AND one column
Row Vector
Example :
>> a = [1, 2 , 3]
a=
1 2 3
Column Vector
gives
Example :
>> a = [1; 2; 3]
a=
1
2
3
gives
Matrix
a = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
a=
1 2 3
4 5 6
7 8 9
3
=
+
* or.*
/ or ./
^ or .^
a = b (assign b to a)
a+b
a -b
a*b or a.*b
a/b or a./b
a^b or a.^b
Matlab Matrices
Accessing Single Elements of a Matrix
A(i,j)
Accessing Multiple Elements of a Matrix
A(1,4) + A(2,4) + A(3,4) + A(4,4)
Deleting Rows and Columns
To delete the second column of X, use X(:,2) = []
Concatenating
C=[A;B]
fliplr(X) -- returns X with row preserved and columns flipped in the left/right direction.
flipud(X) -- returns X with columns preserved and rows flipped in the up/down direction.
flipdim(X,Dim) -- Flip matrix along specified dimension
rot90() -- is the 90 degree counter clockwise rotation of matrix A.
tril
-- the lower triangular part of X
triu
-- the upper triangular part of X.
Linspace Function
Generates a vector of evenly spaced points between two end points,
linspace(start,stop,npoints ):
x = linspace(0,1,10)
x=
Columns 1 through 7
0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667
Columns 8 through 10
0.7778 0.8889 1.0000
Repmat function
The repmat function can be used to replicate a matrix:
a = [1 2; 3 4]
repmat(a,2,3)
ans =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
10
maximum
minimum
find indices of nonzero elements
average or mean
median
standard deviation
sort in ascending order
sort rows in ascending order
sum of elements
product of elements
difference between elements
trapezoidal integration
cumulative sum
cumulative product
cumulative trapez integration
11
Find function
I = FIND(X) returns the linear indices corresponding to the nonzero entries of the array X. X
may be a logical expression.
I = FIND(X,K) returns at most the first K indices corresponding to the nonzero entries of the
array X. K must be a positive integer, but can be of any numeric type.
12
13
14
15
Matlab Branching
if condition
statements A
else
statement B
end
If else
If elseif-else
if condition1
statement A
elseif condition2
statement B
elseif condition3
statement C
...
else
statement E
end
16
Nested ifs
for loop
for index = j:k {for index = j:m:k}
statements
end
index = j:m:k
statements
if condition
break
end
end
17
18
Function format
function variables = function_name(variables)
commands
Commands
Example :
function s = generatesin(fs,f,t)
commands
s1 = generatesin(8000,2,2)
s2 = generatesin(8000,100,0.2)
19
Matlab Graphics
plot(y) - In this case the elements of y are plotted against their indexes
HOLD ON holds the current plot and all axis properties so that subsequent graphing
commands add to the existing graph.
SUBPLOT(m,n,p)
Breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the
current plot.
Example :
subplot(2,1,1), plot(x);
subplot(2,1,2), plot(y);
To save a figure
saveas(gcf,wavFile,'eps')
20
21
Example: Images
Reading the image: s=imread('Lena256.bmp');
Converting the bitmap: im2double(s);
Converting to a displayable structure: mat2gray(S)
Displaying the image: imshow(r3)
Color to gray conversion: rgb2gray(s)
Writing the images: imwrite(s,filename,format)
22