Matlab Fast Manual

© Zhou Qingqing 2001
http://www.cs.toronto.edu/~zhouqq

Last modified: 2001-05-03


1. Basic Commands

... - used to continue the line;
; - suppress the output;
more - more off and more on to control the paging of output;
eps - float precision const = 2.2204e-016;
whos/who - see variable definition;
clear - remove variable from current workspace;
pwd/cd/dir/ls/getenv/! - current directory/change dir/directory listing/directory listing/get environment parameters, usage: getenv('PATH') /execute OS commands

2. Examples

Matlab style for-each loop

% matlab style for-each loop
R = rand(5, 6)
R(1, 3) = NaN
R(2, 5) = NaN
R(5, 1) = NaN
R(isnan(R)) = 0        % for each element r in R, if r is NaN, then r = 0;
R(R==0 | R<0.1 )=1     % for each element r in R, if r == 0 | r <0.1 , then r = 1;

Interpolation

% sample set
t = 1900:10:1990;
p = [75.995 91.972 105.711 123.203 131.669 ...
150.697 179.323 203.212 226.505 249.633];

% generate integer 1900:1:2000
x = linspace(1900, 2000, 101);
% t, p are the basic values, x is the simulated interval
% 'spline' can also be 'line', 'nearest', 'pchip', etc.
y = interp1(t,p,x,'spline');

% draw (t, p) pair with 'o' format as first step
% then draw all the points in interval x
plot(t,p,'o',x,y) 

Distribution

% generate 10000 number ~ N(0, 1), simplified version of function normrnd() 
y = randn(10000,1);
% verify mean, variance, standard deviation
mean(y); var(y); std(y);
% see the distribution of y value in interval [-3, 3], should looks like a hat;
x = -3:0.1:3;
hist(y,x)

Weierstrass Function

times = 100;

sum = 0;
for l = 1:times
  x = 1/times*l;
  for i=1:times
   y(i) = (1/2)^i*sin(16^i*x);
   sum = sum + y(i);
   y(i) = y(i) + sum;
   if i>=2 & y(i)-y(i-1)<inf
     break;
  end
end
r(l) = y(i);
end

x = 1:times;
plot(x', r');

Convergence of Difference Equation

times = 30;

x = 0.3;
for i=1:times
  % Try different 0.3, 3.2, 3.9 ... 
  y = 3.2*x*(1-x);
  x = y;
  r(i) = y;
end

x=1:times;
plot(x', r', '.');

Interface 

 

3. Functions Collection

Here I listed the functions that I have ever used. Here are some sample data that I may used:

vA = [94 197 16 38 99 141 23];
mA = randn(6, 6);

Each function/command consists of two parts, one is the basic meaning and the other is the usage sample. The text after '%%' is the result the matlab returned. 

3.1 Basic

Matlab is case sensitive. 

{pi, inf, NaN} - $\pi$, $\infty$(when 1/0 for example), not-a-number(when 0/0 for example); 

nargin/nargout - number of function input/output arguments;

rand/randn - uniformly/normally random number generator; usage: rand/randn(nrow, ncol); 

{sign, abs, sqrt, round, floor, ceil, exp, log, log2, log10} - signum function returing {+1, 0, -1}; absolute value; square root; round to nearest integer; round towards $-\infty$, round to $+\infty$; exponential base e; natural logarithm; log base 2; log base 10;

{sin, cos, ...} - trigonometric functions; 

 

3.2 GUI Functions

; - suppress output;

disp - display text; usage: disp('some text');

plot - draw 2D picture; usage: ref. to matlab help;

subplot - select part of canvas to do drawing; usage: subplot(nrow, ncol, idx_of_nrow_times_ncol);

hold on/off - hold/release the current canvas; usage: hold on/off;

Example: 

subplot(3,2,1);
plot(vA);
subplot(3,2,5);
hold on;
plot(vA*2);
plot(vA*1.5);
hold off;
subplot(3,2,6);
plot(vA);

 

3.3 Vector Functions

{+, -, .*, .^, ./, .\} - Vector addition; subtraction; multiplication; power; right division; left division;

length - length of vector; usage: len = length(vA) %% 7;

 

3.4 Matrix Functions

{+, -, *, ^, /, \, '} - Vector addition; subtraction; multiplication; power; right division; left division; transpose;

size - size of matrix; usage: [nrow, ncol] = size(mA) %% nrow=6, ncol=6;

zeros/ones/eye/magic - matrix with all 0; matrix with all 1; matrix with diagonal 1; magic matrix; 

 

3.5 Statistical Functions

 

 

 

Links

1. http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml - Matlab help desk all references;

2. http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.shtml - Matlab help desk function reference;

3. http://groups.google.ca/groups?hl=en&lr=lang_zh-CN|lang_en&group=comp.soft-sys.matlab - Matlab newgroup;