# EE 4780 Matlab tutorial

```EE 4780
Matlab tutorial
MATLAB



Matlab help files are helpful to learn it
Exercise:
f = [1 2; 3 4]
g = [1; 1]
g = [1 1]
g’
z = f * g’
n=0:10
plot(sin(n));
plot(n,sin(n)); title(‘Sinusoid’); xlabel(‘n’); ylabel(‘Sin(n)’);
n=0:0.1:10
plot(n,sin(n));
grid;
figure; subplot(2,1,1); plot(n,sin(n)); subplot(2,1,2); plot(n,cos(n));
2
MATLAB

Some more built-ins
a = zeros(3,2)
b = ones(2,4)
c = rand(3,3) %Uniform distribution
help rand
help randn %Normal distribution
d1 = inv(c)
d2 = inv(rand(3,3))
d3 = d1+d2
d4 = d1-d2
d5 = d1*d2
d6 = d1.*d3
e = d6(:)
3
MATLAB

Image processing in Matlab
figure;
imshow(x);
[h,w]=size(x);
y=x(0:h/2,0:w/2);
imwrite(y,’man.tif’);
% To look for a keyword
lookfor resize
x[0,1]
 x[0, 0]
 x[1, 0]
x[1,1]
x[n1 , n2 ]  


 x[ M  1, 0]
x[0, N  1] 
x[1, N  1] 


x[ M  1, N  1] MxN
4
MATLAB


M-file
Save the following as myresize1.m
function [y]=myresize1(x)
% This function downsamples an image by two
[h,w]=size(x);
for i=1:h/2,
for j=1:w/2,
y(i,j) = x(2*i,2*j);
end
end

Compare with myresize2.m
function [y]=myresize2(x)
% This function downsamples an image by two
[h,w]=size(x);
for i=0:h/2-1,
for j=0:w/2-1,
y(i+1,j+1) = x(2*i+1,2*j+1);
end
end

Compare with myresize3.m
function [y]=myresize3(x)
% This function downsamples an image by two
y = x(1:2:end,1:2:end);
