Documente Academic
Documente Profesional
Documente Cultură
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (2) Objects in matlab -- the basic objects in matlab are scalars,
% vectors, and matrices...
N
v
v
v
=
=
=
=
5
[1 0 0]
[1;2;3]
v'
row)
v
= [1:.5:3]
v
= pi*[-4:4]/4
[start:stepsize:end]
v
= []
m
= [1 2 3; 4 5 6]
m
=
v
=
m
=
v
=
(see also
zeros(2,3)
ones(1,3)
eye(3)
rand(3,1)
randn)
load matrix_data
containing:
%
%
%
%
a scalar
a row vector
a column vector
transpose a vector
(row to column or column to
matrix_data
v
v(3)
= [1 2 3];
m
= [1 2 3; 4 5 6]
m(1,3)
%
%
%
2
5
1
3
6
2
4
7
3
columnnumber)
m(2,:)
m(:,1)
size(m)
size(m,1)
size(m,2)
% size of a matrix
% number rows
% number of columns
m1
= zeros(size(m))
who
whos
% list of variables
% list/size/type of variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (3) Simple operations on vectors and matrices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (A) Pointwise (element by element) Operations:
%
%
a
2
a
b
a
a
a
a
a
log( [1 2 3 4] )
round( [1.5 2; 2.2 3.1] )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (B) Vector Operations (no for loops needed)
% Built-in matlab functions operate on vectors, if a matrix is given,
% then the function operates on each column of the matrix
a
= [1 4 6 3]
sum(a)
mean(a)
var(a)
std(a)
%
%
%
%
%
vector
sum of vector elements
mean of vector elements
variance
standard deviation
max(a)
% maximum
a
= [1 2 3; 4 5 6]
a(:)
mean(a)
max(a)
max(max(a))
max(a(:))
%
%
%
%
%
%
matrix
vectorized version of the matrix
mean of each column
max of each column
to obtain max of matrix
or...
%%%%%%%%%%%%%%%%%%%%%%%%
% (C) Matrix Operations:
[1 2 3] * [4 5 6]'
3x1
product
[1 2 3]' * [4 5 6]
1x3
a
b
c
= rand(3,2)
= rand(2,4)
= a * b
a
= [1 2; 3 4; 5 6]
b
= [5 6 7];
b * a
a' * b'
% 3x2 matrix
% 2x4 matrix
% 3x4 matrix
%
%
%
%
3 x 2 matrix
1 x 3 vector
matrix multiply
matrix multiply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(4) Saving your work
save mysession
variables
save mysession a b
clear all
clear a b
load mysession
a
b
% load session
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(5) Relations and control statements
% Example: given a vector v, create a new vector with values equal to
% v if they are greater than 0, and equal to 0 if they less than or
% equal to 0.
v
= [3 5 -2 5 -1 0]
u
= zeros( size(v) );
for i = 1:size(v,2)
% 1: FOR LOOPS
% initialize
% size(v,2) is the number of columns
end
u
v
u2
ind
u2(ind)
=
=
=
=
[3 5 -2 5 -1 0]
zeros( size(v) );
find( v>0 )
v( ind )
% 2: NO FOR LOOPS
% initialize
% index into >0 elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(6) Creating functions using m-files:
% Functions in matlab are written in m-files. Create a file called
% 'thres.m' In this file put the following 4 lines:
function res = thres( v )
u
= zeros( size(v) );
ind
= find( v>0 )
u(ind) = v( ind )
v
= [3 5 -2 5 -1 0]
thres( v )
% initialize
% index into >0 elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(7) Plotting
x
= [0 1 2 3 4];
plot( x );
plot( x, 2*x );
axis( [0 8 0 8] );
% basic plotting
x
= pi*[-24:24]/24;
plot( x, sin(x) );
xlabel( 'radians' );
ylabel( 'sin value' );
title( 'dummy' );
gtext( 'put cursor where you want text and press mouse' );
figure;
graphs
subplot( 1,2,1 );
plot( x, sin(x) );
axis square;
subplot( 1,2,2 );
plot( x, 2.*cos(x) );
axis square;
figure;
plot( x,sin(x) );
hold on;
top
plot (x, 2.*cos(x), '--' );
legend( 'sin', 'cos' );
hold off;
figure;
m = rand(64,64);
imagesc(m)
colormap gray;
axis image
axis off;
% matrices as images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(8) Working with the Images and the Matlab Image Processing Toolbox
[I,map]=imread('trees.tif');
stored images
figure
imshow(I,map)
w/colormap
I2=ind2gray(I,map);
% convert it to grayscale
figure
imagesc(I2,[0 1])
colormap('gray')
axis('image')
proportional
to image dimensions
I=imread('football.jpg');
figure
imshow(I)
rect=getrect;
I2=imcrop(I,rect);
I2=rgb2gray(I2);
grayscale
imagesc(I2)
% select rectangle
% crop
% convert cropped image to
colormap('gray')
colorbar
impixelinfo
interactively
truesize
screen pixel
truesize(2*size(I2))
screen pixels
I3=imresize(I2,0.5,'bil');
I3=imrotate(I2,45,'bil','crop');
I3=double(I2);
allow
imagesc(I3.^2)
wise)
imagesc(log(I3))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%