GEOS 597e Spatiotemporal Data Analysis Workshop
Useful MATLAB functions compiled by STDA 2006 [updated 8/30/06]
|
one-line synopsis |
simple example |
|
|
specify the axis extremes on a plot |
plot(1:10,1:10);axis([0 11 0 11]) |
|
|
Removes item(s) from the workspace |
clear or clear x |
|
|
convolution and polynomial multiplication |
w = conv(u,v) |
|
|
cosine(radians) |
y = cos(x); |
|
|
remove linear trends from variable |
y = detrend(x) |
|
|
either creates diagonal matrix or returns diagonal of matrix |
create diagonal matrix: y= diag(v,k) return diagonal of matrix: y = diag(x) |
|
|
Finds indices and values of nonzero elements |
ind = find(x) |
|
|
flips each column in a matrix upsidedown |
A = [1;2;3] ; B = flipud(A) |
|
|
run a set of commands a given number of times |
for m = [1:5] ;x = [ m+2./m] ; end |
|
|
turns grid lines on graph on/off; places gridlines on new graph |
plot (1:5, 1:5) ; grid |
|
|
runs commands if a specified condition(s) is met |
|
|
|
finds the specified common elements of matrices |
A = [1 2 3 5 7 11] ; B = [2 4 6 8 10 12] ; intersect (A, B) |
|
|
returns the inverse of a matrix where AA-1=1 |
A = [1 5 3; 14 15 16; 71 82 93;] ; inv(A) |
|
|
True-false (TF) function indicating whether elements of an array are not-numbers |
a = [-1 0 1]; isnan(2/a) |
|
|
specifies the length of a vector |
a = [1 2 3 4 5]; n = length(a) |
|
|
returns the mean of an array |
mean(a) |
|
|
transforms the x, y, and z variables into a grid for making mesh plots |
[X, Y, Z] = meshgrid(x, y, z) |
|
|
controls how much output in show in the Command Window, including number of lines (n) |
more(n) |
|
|
returns an array of ones |
n = 7; y = ones(n) |
|
|
plots all lines defined by |
x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y)
|
|
|
|
You can print a non-current figure by specifying the figure's handle. If a figure has the title "Figure 2", its handle is 2. The syntax is
This example prints the figure whose handle is 2, regardless of which figure is the current figure.
|
|
|
this function generates random numbers |
|
|
|
changes the dimensions of a matrix (the new/old dimensions have to match) |
B = reshape(A,2,6) |
|
|
plots y data in a logarithmic scale and x data in a linear scale |
x = 0:.1:10; semilogy(x,10.^x) |
|
|
tells you the dimensions of a matrix or vector |
size(A) |
|
|
returns sine of elements (in radians) of an array |
x = [0:pi/2:2*pi]; y = sin(x) %y = [0,1,0,-1,0] |
|
|
sorts array elements in ascending or descending order |
x = [10:-1:0]; y = sort(x) %y = [0:10] |
|
|
sorts rows of an array in ascending order |
x =[2,1,0;3,0,0;2,1,1]; y = sortrows(x) % y = [2,1,0;2,1,1;3,0,0] |
|
|
returns the square root of each element in an array |
X = [-1:1]; sqrt(X) |
|
|
returns an array B with the same elements as A, but removes any dimensions where size(A, dim) = 1 |
Y = rand(4,1,4); Z = squeeze(Y) gives dimensions of Z as a 4x4 matrix |
|
|
divides the current plot figure into smaller sections for visualization of multiple plots within same window |
Subplot(m,n,p) = subplot(2,2,1) gives a 2x2 matrix of plots, and plots this figure in the first space (upper left) |
|
|
returns the sum of the elements in a vector, or the sum of the elements of a matrix as row vectors |
s = sum(A) |
|
|
performs a single value decomposition of a matrix |
s= svd(X) |
|
|
Measures the performance of a program using a timer |
tic (starts timer) (statements to be performed) toc (prints time elapsed) |
|
|
sum diagonal elements of a matrix |
b = trace(A) |
|
|
combine two vectors with no repetitions |
A=[0 3 5 7]; B=[0 2 4 7]; C=union(A,B); C= 0 2 3 4 5 7 |
|
|
label the x-axis |
xlabel('temperature (K)') |
|
|
label the y-axis |
ylabel('year') |
|
|
list variables in workspace |
who, who a* (show variable names starting with "a") |
|
|
creates a matrix whose elements are zero |
x = zeros(3,2), x = zeros(4,5,'int8') |
Back to Schedule/Syllabus.