Sunteți pe pagina 1din 5

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Introducing Matlab (adapted from http://www.cns.nyu.edu/~eero and


% http://www.cs.dartmouth.edu/~farid/teaching/cs88/matlab.intro.html)
% via http://www-cse.ucsd.edu/%7Esjb/classes/matlab/matlab.intro.html
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (1) Help and basics
% The symbol "%" is used in front of a comment.
% To get help type "help" (will give list of help topics) or "help topic"
% If you don't know the exact name of the topic or command you are looking
for,
% type "lookfor keyword" (e.g., "lookfor regression")
% When writing a long matlab statement that exceeds a single row use ...
% to continue statement to next row.
% When using the command line, a ";" at the end means matlab will not
% display the result. If ";" is omitted then matlab will display result.
% Use the up-arrow to recall commands without retyping them (and down
% arrow to go forward in commands).
%
%
%
%
%
%

Other commands borrowed from emacs and/or tcsh:


C-a moves to beginning of line (C-e for end), C-f moves forward a
character (C-b moves back), C-d deletes a character, C-k deletes
the line to the right of the cursor, C-p goes back through the
command history and C-n goes forward (equivalent to up and down arrows),
tab command completion.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (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

% a vector in a specified range:


%
[start:end] or
% empty vector
%
%
%
%
%
%

a matrix: 1ST parameter is ROWS


2ND parameter is COLS
a matrix of zeros
a matrix of ones
identity matrix
random matrix with values in [0,1]

% read data from a file:


% create a file 'matrix_data'

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

% access a vector element


%
vector(number)
% Index starts from 1

% access a matrix element


%
matrix(rownumber,

columnnumber)
m(2,:)
m(:,1)

% access a matrix row (2nd row)


% access a matrix column (1st row)

size(m)
size(m,1)
size(m,2)

% size of a matrix
% number rows
% number of columns

m1

% create a new matrix with size of m

= 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

addition of vectors/matrices and multiplication by a scalar


are done "element by element"
= [1 2 3 4];
% vector
* a
% scalar multiplication
/ 4
% scalar multiplication
= [5 6 7 8];
% vector
+ b
% pointwise vector addition
- b
% pointwise vector addition
.^ 2
% pointise vector squaring (note .)
.* b
% pointwise vector multiply (note .)
./ b
% pointwise vector divide (note .)

log( [1 2 3 4] )
round( [1.5 2; 2.2 3.1] )

% pointwise arithmetic operation


% pointwise arithmetic operation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (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

% row vector 1x3 times column vector


% results in single number, also
% known as dot product or inner

product
[1 2 3]' * [4 5 6]
1x3

% column vector 3x1 times row vector


% results in 3x3 matrix, also
% known as outer product

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

% creates mysession.mat with all

clear all
clear a b

% clear all variables


% clear variables a and b

load mysession
a
b

% load session

% save only variables a and b

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(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)

if( v(i) > 0 )


u(i) = v(i);
end

=
=
=
=

[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

% call from command line

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(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;

% multiple functions in separate

figure;
plot( x,sin(x) );
hold on;
top
plot (x, 2.*cos(x), '--' );
legend( 'sin', 'cos' );
hold off;

% multiple functions in single graph


% hold on tells matlab to write on
% of the current plot

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

% use as it is, Matlab has pre-

figure
imshow(I,map)
w/colormap

% display it as indexed image

I2=ind2gray(I,map);

% convert it to grayscale

figure
imagesc(I2,[0 1])
colormap('gray')
axis('image')
proportional

% scale data to use full colormap


% for values between 0 and 1
% use gray colormap
% make displayed aspect ratio
%

to image dimensions

I=imread('football.jpg');

% read a JPEG image into 3D array

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

% scale data to use full colormap


% between min and max values in I2
% turn on color bar
% display pixel values
% display at resolution of one
% per image pixel
% display at resolution of two
%

I3=imresize(I2,0.5,'bil');
I3=imrotate(I2,45,'bil','crop');
I3=double(I2);
allow
imagesc(I3.^2)
wise)
imagesc(log(I3))

per image pixel

% resize by 50% using bilinear


% interpolation
% rotate 45 degrees and crop to
% original size
% convert from uint8 to double, to
% math operations
% display squared image (pixel% display log of image

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

S-ar putea să vă placă și