GEOS 597e Spatiotemporal Data Analysis Workshop

Useful MATLAB functions compiled by STDA 2006 [updated 8/30/06]

function and link

one-line synopsis

simple example

axis

specify the axis extremes on a plot

plot(1:10,1:10);axis([0 11 0 11])

clear

Removes item(s) from the workspace

clear or clear x

conv

convolution and polynomial multiplication

w = conv(u,v)

cos

cosine(radians)

y = cos(x);

detrend

remove linear trends from variable

y = detrend(x)

diag

either creates diagonal matrix or returns diagonal of matrix

create diagonal matrix: y= diag(v,k)

return diagonal of matrix: y = diag(x)

find

Finds indices and values of nonzero elements

ind = find(x)

flipud

flips each column in a matrix upsidedown

A = [1;2;3] ; B = flipud(A)

for ... end

run a set of commands a given number of times

for m = [1:5] ;x = [ m+2./m] ; end

grid

turns grid lines on graph on/off; places gridlines on new graph

plot (1:5, 1:5) ; grid

if ... end

runs commands if a specified condition(s) is met


N = [1:10];M=[3:12]; if N<M ; A = [N+M; M]; end

intersect

finds the specified common elements of matrices

A = [1 2 3 5 7 11] ; B = [2 4 6 8 10 12] ; intersect (A, B)

inv

returns the inverse of a matrix where AA-1=1

A = [1 5 3; 14 15 16; 71 82 93;] ; inv(A)

isnan

True-false (TF) function indicating whether elements of an array are not-numbers

a = [-1 0 1]; isnan(2/a)

length

specifies the length of a vector

a = [1 2 3 4 5]; n = length(a)

mean

returns the mean of an array

mean(a)

meshgrid

transforms the x, y, and z variables into a grid for making mesh plots

[X, Y, Z] = meshgrid(x, y, z)

more [on/off]

controls how much output in show in the Command Window, including number of lines (n)

more(n)

ones

returns an array of ones

n = 7; y = ones(n)

plot

plots all lines defined by Xn versus Yn pairs.

x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y)


print

sends the contents of the current figure to the printer

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

  • print -fhandle 

This example prints the figure whose handle is 2, regardless of which figure is the current figure.

  • print -f2

randn

this function generates random numbers

R = randn(3,4),

reshape

changes the dimensions of a matrix (the new/old dimensions have to match)

B = reshape(A,2,6)

semilogy

plots y data in a logarithmic scale and x data in a linear scale

x = 0:.1:10;
semilogy(x,10.^x)

size

tells you the dimensions of a matrix or vector

size(A)

sin

returns sine of elements (in radians) of an array

x = [0:pi/2:2*pi]; y = sin(x) %y = [0,1,0,-1,0]

sort

sorts array elements in ascending or descending order

x = [10:-1:0]; y = sort(x) %y = [0:10]

sortrows

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]

sqrt

returns the square root of each element in an array

X = [-1:1]; sqrt(X)

squeeze

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

subplot

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)

sum

returns the sum of the elements in a vector, or the sum of the elements of a matrix as row vectors

s = sum(A)

svd

performs a single value decomposition of a matrix

s= svd(X)

tic ... toc

Measures the performance of a program using a timer

tic (starts timer)

(statements to be performed)

toc (prints time elapsed)

trace

sum diagonal elements of a matrix

b = trace(A)

union

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

xlabel

label the x-axis

xlabel('temperature (K)')

ylabel

label the y-axis

ylabel('year')

who

list variables in workspace

who, who a* (show variable names starting with "a")

zeros

creates a matrix whose elements are zero

x = zeros(3,2), x = zeros(4,5,'int8')



Back to Schedule/Syllabus.