You are on page 1of 4

SIGNAL AND SYSTEMS LABORATORY

By: Abdulbasit Sabaawi

Experiment [3]
Matrix Functions

Functions for Creating Matrices:

zeros Create an array of all zeros


ones Create an array of all ones
eye Identity matrix
rand Uniformly distributed random elements
randn Normally distributed random elements
magic Create special matrix

Functions for Matrices:


max Find the maximum of each colomn
min Find the minimum of each colomn
sum Find the summation of each colomn
Transpose (') Transpose the matrix
inv Matrix inverse
diag Diagonal of matrix
trace Summation of diagonal matrix
size Return array dimentions
fliplr Flip matrix left-right
flipud Flip matrix up-down
repmat Replicate and tile matrix
tril Lower triangular part of the matrix
triu Upper triangular part of the matrix
Rot90(A) Rotates matrix A counterclockwise by 90 degrees

1
SIGNAL AND SYSTEMS LABORATORY

Rot90(A, k) Rotates matrix A counterclockwise by k*90 degrees,


where k is an integer
fix Remove the fractional part from the matrix elements

Let us verify that using MATLAB. The first statement to try is


Let A=
16 3 2 13
5 10 11 8
9 7 6 9
4 15 14 1

>>sum(A)
ans=
34 35 33 31

>>A'
ans=
16 5 9 4
3 10 7 15
2 11 6 14
13 8 9 1

>>diag(A)
ans=
16
10
6
1

>>tril(A)
ans=
16 0 0 0
5 10 0 0
9 7 6 0
4 15 14 1

>>triu(A)
ans=
16 3 2 13
0 10 11 8
0 0 6 9
2
SIGNAL AND SYSTEMS LABORATORY

0 0 0 1

>> size(A)
ans=
4 4 (means that A has 4 rows and 4 columns)
>> fliplr(A)
ans =
13 2 3 16
8 11 10 5
9 6 7 9
1 14 15 4

>> flipud(A)

ans =
4 15 14 1
9 7 6 9
5 10 11 8
16 3 2 13

>>z = zeros(2,4)
z=
0 0 0 0
0 0 0 0

>> [m,n]=size(A)
m=
4
n=
4

>> f=5*ones(3,3)

f=
5 5 5
5 5 5
5 5 5

>> N=fix(10*rand(1,10))

N=
9 2 6 4 8 7 4 0 8 4

3
SIGNAL AND SYSTEMS LABORATORY

>>R=randn(1,4)
R=

0.2573 -1.0565 1.4151 -0.8051

Experiment:
Q1) What is the result of the following command:
>> Sum (A’)
>> Sum (A)’
>> Sum (A’)’
>> Sum (diag (A))
>> Sum (triu (fliplr (A’)))
>> Sum (diag (fliplr (A’)’))
>>Sum (tril(fliplr (A’)’))
>> Sum (diag (fliplr (A’)’)’)
>> Sum (diag (flipud (A’)’)’)’
Q2) Given the array:
A=
1 4 3 2
4 1 2 5
3 3 5 1

Find the average of each column in A using MATLAB matrix functions.

You might also like