Matlab Basic Commands
Matlab Basic Commands
intro.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% Introduction to Matlab
% (adapted from
http://www.stanford.edu/class/cs223b/matlabIntro.html)
%
% Stefan Roth <roth (AT) cs DOT brown DOT edu>, 09/08/2003
% Last modified: 09/10/2003
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% (1) Basics
A = [1, 2; ...
3, 4];
% Simple debugging:
% If the command "dbstop if error" is issued before running a script
% or a function that causes a run-time error, the execution will stop
% at the point where the error occurred. Very useful for tracking
down
% errors.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% (2) Basic types in Matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (A) The basic types in Matlab are scalars (usually double-precision
% floating point), vectors, and matrices:
N = 5 % A scalar
v = [1 0 0] % A row vector
v = [1; 2; 3] % A column vector
v = v' % Transpose a vector (row to column or
% column to row)
v = 1:.5:3 % A vector filled in a specified range:
v = pi*[-4:4]/4 % [start:stepsize:end]
% (brackets are optional)
v = [] % Empty vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (B) Creating special matrices: 1ST parameter is ROWS,
% 2ND parameter is COLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (C) Indexing vectors and matrices.
% Warning: Indices always start at 1 and *NOT* at 0!
v = [1 2 3];
v(3) % Access a vector element
m = [1 2 3 4; 5 7 8 8; 9 10 11 12; 13 14 15 16]
m(1, 3) % Access a matrix element
% matrix(ROW #, COLUMN #)
m(2, :) % Access a whole matrix row (2nd row)
m(:, 1) % Access a whole matrix column (1st
column)
m = [1 2 3; 4 5 6]
size(m) % Returns the size of a matrix
size(m, 1) % Number of rows
size(m, 2) % Number of columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% (3) Simple operations on vectors and matrices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (A) Element-wise operations:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (B) Vector Operations
% Built-in Matlab functions that operate on vectors
a = [1 4 6 3] % A row vector
sum(a) % Sum of vector elements
mean(a) % Mean of vector elements
var(a) % Variance of elements
std(a) % Standard deviation
max(a) % Maximum
min(a) % Minimum
% If a matrix is given, then these functions will operate on each
column
% of the matrix and return a row vector as result
a = [1 2 3; 4 5 6] % A matrix
mean(a) % Mean of each column
max(a) % Max of each column
max(max(a)) % Obtaining the max of a matrix
mean(a, 2) % Mean of each row (second argument
specifies
% dimension along which operation is
taken)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (C) Matrix Operations:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (D) Reshaping and assembling matrices:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% (4) Control statements & vectorization
% Warning:
% Loops run very slowly in Matlab, because of interpretation
overhead.
% This has gotten somewhat better in version 6.5, but you should
% nevertheless try to avoid them by "vectorizing" the computation,
% i.e. by rewriting the code in form of matrix operations. This is
% illustrated in some examples below.
% Examples:
for i=1:2:7 % Loop from 1 to 7 in steps of 2
i % Print i
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%(6) Creating scripts or functions using m-files:
%
% Matlab scripts are files with ".m" extension containing Matlab
% commands. Variables in a script file are global and will change
the
% value of variables of the same name in the environment of the
current
% Matlab session. A script with name "script1.m" can be invoked by
% typing "script1" in the command window.
% Functions are also m-files. The first line in a function file must
be
% of this form:
% function [outarg_1, ..., outarg_m] = myfunction(inarg_1, ...,
inarg_n)
%
% The function name should be the same as that of the file
% (i.e. function "myfunction" should be saved in file
"myfunction.m").
% Have a look at myfunction.m and myotherfunction.m for examples.
%
% Functions are executed using local workspaces: there is no risk of
% conflicts with the variables in the main workspace. At the end of a
% function execution only the output arguments will be visible in the
% main workspace.
[c, d] = ...
myotherfunction(a, b) % Call myotherfunction with two return
% values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%(7) Plotting
x = [0 1 2 3 4]; % Basic plotting
plot(x); % Plot x versus its index values
pause % Wait for key press
plot(x, 2*x); % Plot 2*x versus x
axis([0 8 0 8]); % Adjust visible rectangle
figure;
subplot(1, 2, 1); % Multiple functions in separate graphs
plot(x, sin(x)); % (see "help subplot")
axis square; % Make visible area square
subplot(1, 2, 2);
plot(x, 2*cos(x));
axis square;
figure;
plot(x, sin(x));
hold on; % Multiple functions in single graph
plot(x, 2*cos(x), '--'); % '--' chooses different line pattern
legend('sin', 'cos'); % Assigns names to each plot
hold off; % Stop putting multiple figures in
current
% graph
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%(8) Working with (gray level) images
figure
imagesc(I) % Display it as gray level image
colormap gray;
figure;
g = [1 2 1]' * [1 2 1] / 16; % 3x3 Gaussian filter mask
I2 = double(I); % Convert image to floating point
I3 = conv2(I2, g); % Convolve image with filter mask
I3 = conv2(I2, g, 'same'); % Convolve image, but keep original
size
subplot(1, 2, 1) % Display original and filtered image
imagesc(I); % side-by-side
axis square;
colormap gray;
subplot(1, 2, 2)
imagesc(I3);
axis square;
colormap gray;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
myfunction.m
function y = myfunction(x)
% Function of one argument with one return value
myotherfunction.m
y = a + b;
z = a - b;