Professional Documents
Culture Documents
Built-in Functions:
There are two parts to functions: the name and the argument. The name of a function follows the
same general rules as variable names. Every function name is followed by a set of parentheses,
which is where you enter the argument. Look at the list of commonly used Matlab functions
(credit to Dr. Brian Vick of the MechE Dept at Virginia Tech).
Arrays:
There are three common data types in Matlab: strings, scalars, and arrays. We will cover strings
later. Scalars are singular numbers, like 4, 17, or 39. Arrays are several numbers, in an array or
matrix. Use square brackets to create arrays. Type x = [3 5] and see that 3 and 5 are in the
same row. To make a new column, use a semi-colon. Type x = [3; 5] to see that 3 and 5 are in
the same row. To find the transverse of any matrix (flip it about the diagonal) just type the name
of the matrix then use an apostrophe. Type x = [3 5], then hit enter and type x and youll
see you get the same result as x = [3; 5].
For longer vectors, you can get a range by using a colon. Type y = 1:10 to get an array of all
the values from 1 to 10. A colon has a default spacing (or step size) of 1, but you can change that
by putting in two colons and declaring your step size in between the colons. Type y = 1:.5:5 to
get all the values from 1 to 5 by a step size of .5.
Array Indexing:
Sometimes you want to get only a specific bit of information from a huge array. You can specify
which elements of the array by giving the coordinates of the information. Type the name of
array, followed by parentheses with row number, comma, column number. Ex: array(3,4) gets
the item in the third row and fourth column. To grab more than one item in any direction, you
can enter an array as an argument, such as array(2:4,4) which would grab elements in the
second through fourth rows in the fourth column. If you wanted the whole row or column, enter
a colon in the argument, or if you wanted the last element enter end.
You can also change values of an array using indexing. Set an element of array to a value by
typing the elements index and assigning it a value. Ex: array(2,3) = 15.
Strings:
Scalars and arrays deal with numbers, but strings are for text. Every string is surrounded by
either a single or double quote ( or ).
Logical Statements/Operators:
In computer language, true is 1 and false is 0. You can test if two scalars or arrays are less than,
greater than, equal to (which is == by the way). More information on the second page of Matlab
functions I gave you. These are important to know, because you will use them a lot in your
conditional statements.
Conditional Statements:
(*Warning* You can get into trouble quickly with ill-defined loops. If something goes wrong,
type Ctrl+C to cancel all operations.)
There are three main types: if else statements, for loops, and while loops. If else statements work
like this:
if (something is true)
do this
elseif (something else is true)
do this instead
else
do this as a default
end
Note that I left else to send me a warning message. This is good practice when youre new to if
else statements.
For loops are loops that will perform a set of instructions for
a set number of times. For loops work like this:
Graphing/Plotting:
In general, just use the plot function.
plot(x_variable, y_variable, z_variable, colorsymbolline)
Add the z variable only if you need it, and refer to the other
pages for color, symbol, and line identifiers.
Otherwise, use hist() or surf() for histograms and 3D surface plots respectively.
When youre bored, type these into CW: why, fifteen, xpbomb
General Purpose Commands
MATLAB Commands 3
Programming
Logical Functions
any True if any elements are nonzero.
all True if all elements are nonzero.
find Finds indices of nonzero elements.
finite True if elements are finite.
isnan True if elements are undefined.
isinf True if elements are infinite.
isempty True if matrix is empty.
isreal True if all elements are real.
MATLAB Commands 10
Vector, Matrix and Array Commands
Array Commands
cat Concatenates arrays.
find Finds indices of nonzero elements.
length Computers number of elements.
linspace Creates regularly spaced vector.
logspace Creates logarithmically spaced vector.
max Returns largest element.
min Returns smallest element.
prod Product of each column.
reshape Change size
size Computes array size.
sort Sorts each column.
sum Sums each column.
Special Matrices
eye Creates an identity matrix.
ones Creates an array of ones.
zeros Creates an array of zeros.
Matrix Arithmetic
cross Computes cross products.
dot Computes dot products.
MATLAB Commands 6
Plotting Commands
MATLAB Commands 8
Colors, Symbols and Line Types
Histogram Functions
bar Creates a bar chart.
hist Aggregates the data into equally spaced bins.
histc Aggregates the data into unequally spaced bins.
MATLAB Commands 9