• Uncategorized
• 0

# Matlab Tips and Tricks-2

Clearly, it is important to know the language you intend to use. The language is described in the
manuals so I won’t repeat what they say, but I encourage you to type
help ops
help relop
help arith
help slash
at the command prompt and take a look at the list of operators, functions and special characters, and
look at the associated help pages.
When manipulating arrays in M AT L A B there are some operators and functions that are particu-
larely useful.
2.1 Operators
In addition to the arithmetic operators, M AT L A B provides a couple of other useful operators
The colon operator.
:
Non-conjugate transpose.
.”
Complex conjugate transpose.

Built-in functions
True if all elements of a vector are nonzero.
all
True if any element of a vector is nonzero.
any
Cumulative sum of elements.
cumsum
Diagonal matrices and diagonals of a matrix.
diag
Difference and approximate derivative.
diff
Last index in an indexing expression.
end
Identity matrix.
eye
Find indices of nonzero elements.
find
True for empty matrix.
isempty
True if arrays are numerically equal.
isequal
True for ï¬nite elements.
isfinite
True for inï¬nite elements.
isinf
True for logical array.
islogical
True for Not-a-Number.
isnan
True for numeric arrays.
isnumeric
Length of vector.
length
Convert numeric values to logical.
logical
Number of dimensions.
ndims
Number of elements in a matrix.
numel
Ones array.
ones
Permute array dimensions.
permute
Product of elements.
prod
Change size.
reshape
Size of matrix.
size
Sort in ascending order.
sort
Sum of elements.
sum
Extract lower triangular part.
tril
Extract upper triangular part.
triu
Zeros array.
zeros

Some of these functions are shorthands for combinations of other built-in functions, lik
is
length(x) max(size(x))
is
ndims(x) length(size(x))
is
numel(x) prod(size(x))
Others are shorthands for frequently used tests, like
is
isempty(x) numel(x) == 0
is
isinf(x) abs(x) == Inf
is
isfinite(x) abs(x) ~= Inf
Others are shorthands for frequently used functions which could have been written with low-level
code, like diag, eye, find, sum, cumsum, cumprod, sort, tril, triu, etc.

M-ï¬le functions
Flip matrix along speciï¬ed dimension.
flipdim
Flip matrix in left/right direction.
fliplr
Flip matrix in up/down direction.
flipud
Multiple subscripts from linear index.
ind2sub
Inverse permute array dimensions.
ipermute
Kronecker tensor product.
kron
Linearly spaced vector.
linspace
Generation of arrays for N-D functions and interpolation.
ndgrid
Replicate and tile an array.
repmat
Rotate matrix 90 degrees.
rot90
Shift dimensions.
shiftdim
Remove singleton dimensions.
squeeze
Linear index from multiple subscripts.
sub2ind