MATLAB Reference Card
MATLAB Reference Card
Variable Assignment
Variable Information
Defines a row vector x (horizontal)
Defines a column vector x (vertical)
The range of integers a ... c, equivalent to [a,a+1,...c-1,c]
The range of a ... c, with spacing b, equivalent to [a,a+b,...c-b,c]
The range of a ... c with n equally spaced values in between
An matrix of zeros (m is vertical columns, n is horizontal rows)
An matrix of ones
An matrix of uniformly distributed random numbers [1, +1]
An matrix of random numbers from ( = 0, = 1)
Defines x as the string string (double quotes are never used)
x=[1,2,3,4...];
x=[1;2;3;4...];
a:c
a:b:c
linspace(a,c,n)
zeros(m,n)
ones(m,n)
rand(m,n)
randn(m,n)
x = string
Variable Indexing
Vectors
First element
nth element
Last element
First n elements
Last n+1 elements
Specified list of elements
All elements of x greater than 0
All elements of x between 0 and 9
x(1)
x(n)
x(end)
x(1:n)
x(end-n:end)
x([1,3,6])
x(x>0)
x(x>0 & x<9)
Matrices
Element at row i (vertical indexed) and column j (horizontal indexed)
All of Row i
All of Column j
First m rows
First n columns
The last element in the last row
Transformed full matrix to a column vector (column by column)
x(i,j)
x(i,:)
x(:,j)
x(1:m,:)
x(:,1:n)
x(end,end)
x(:)
Variable Manipulation
x(n) = [];
x(:,n) = [];
x
x.
max(x) min(x)
max(x,[],c)
[a,i] = max(x)
sort(x)
sort(x,c)
unique(x)
find(x == a)
reshape(x,[m,n])
cat(c,x,y)
2016
Jesse Knight
length(x)
s = size(x)
size(x,c)
numel(x)
Matrix Computations
a+b
a-b
a.*b
a*b
a./b
a/b
a.^b
Math Operations
sin, cos, tan, asin, acos, atan, log, log10, exp, sqrt, ...
Standard functions, always element-wise operation
sum(x)
Sum of elements
sum(x,c)
Sum of elements of x, along the dimension c
prod(x)
Product of elements of x
diff(x)
Difference between every element of x (yields length n-1)
cumsum(x)
Cumulative sum of the elements in x
mean(x)
Mean of the elements in x
median(x)
Median of the elements in x
log(x,b)
Logarithm of x with base b
real(x)
Real part of all elements in x
imag(x)
Imaginary part of all elements in x
abs(x)
Absolute value, or magnitude if x is complex
angle(x)
Angle in radians of the complex number(s) x
mod(x,b)
Modulus (remainder) of (/ )
Constants
i or j
pi
Inf
NaN
exp(1)
Less than
Less than or equal to
Greater than
Greater than or equal to
Equal to
Not equal to
And (element-wise) And (single value)
Or (element-wise) Or (single value)
Not
true if any result in an element-wise expression is true
true if all results of an element-wise expression are true
based on MATLAB Reference Cards by Thomas Pingel, Dept. of Geography, Northern Illinois University, & Carly Fennell, School of Engineering, University of Guelph
M AT L A B R E F E R E N C E C A R D I I
Documentation
Scripting
Displays a description of the <function> and how to use it
More detailed information than help
help <function>
doc <function>
Workspace
cd(str)
addpath(str)
clc
who
clear x
clear
clearvars except x
save(name)
save(name,a...)
load(name)
load(name,a...)
Programming Constructs
x = [...];
s.x = x;
Structs: Can group many variables (e.g. x) into one (e.g. s) using .
notation; structs can also be multidimensional (e.g. s(2,3).x = 6;).
switch (...)
case (...)
...
case (...)
...
otherwise
...
end
for i = 1:n
...
end
while (...)
...
end
2016
Jesse Knight
<name of script>
<line of code>;
%<line of text>
keyboard
return or dbcont
...
CTRL+c
Formatting Output
fprintf(fmt,vars...)
sprintf(fmt,vars...)
error(msg)
warning(msg)
Figures
h = figure(n)
h = gcf
h = subplot(m,n,k)
h = gca
get(h)
set(h,<prop>,x)
hold(on)
hold(off)
print depsc2 f1.eps
close(n)
close(all)
Plotting
plot(y)
plot(x,y)
h = plot(...)
stem(y)
hist(x)
hist(x,n)
title(str)
xlabel(str)
xlim([xmin,xmax])
Images
I = imread(str)
imshow(I)
imshow(I,[])
imshow(I,hot)
imwrite(I,str,fmt)
based on MATLAB Reference Cards by Thomas Pingel, Dept. of Geography, Northern Illinois University, & Carly Fennell, School of Engineering, University of Guelph