Вы находитесь на странице: 1из 4

Basic Operations on Matrices

All the operators in MATLAB defined on


matrices : +, -, *, /, ^, sqrt, sin, cos etc.
Element wise operators defined with
preceding dot : .*, ./, .^ .
size(A) - size vector
sum(A) - columns sums vector
sum(sum(A)) - all the elements sum
Logical Conditions
==, <, >, (not equal)~=,(not)~
find(condition) - Returns indexes of As
elements that satisfies the condition.
Flow Control
MATLAB has five flow control constructs:
if statements
switch statements
for loops
while loops
break statements
if
IF statement condition.
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
if(cont.)
Example:
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;End
switch
SWITCH - Switch among several cases based on expression.
The general form of the SWITCH statement is:
SWITCH switch_expr
CASE case_expr,
statement, ..., statement
CASE {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
OTHERWISE,
statement, ..., statement
END
switch (cont.)
Note:
Only the statements between the matching CASE and the next CASE,
OTHERWISE, or END are executed.
Unlike C, the SWITCH statement does not fall through
(so BREAKs are unnecessary).
for (cont.)
Example:
FOR I = 1:N,
FOR J = 1:N,
A(I,J) = 1/(I+J-1);
END
END
while (cont.)
Example:
E = 0*A; F = E + eye(size(E)); N = 1;
while norm(E+F-E,1) > 0,
E = E + F;
F = A*F/N;
N = N + 1;
End
Scripts and Functions
There are two kinds of M-files:
Scripts, which do not accept input arguments or return output arguments. They
operate on data in the workspace.
Functions, which can accept input arguments and return output arguments.
Internal variables are local to the function.
What is MATLAB?
MATLAB (MATrix LABoratory) is a tool for numerical computation and
visualization. The basic data element is a matrix, so if you need a program that
manipulates array-based data it is generally fast to write and run in MATLAB
(unless you have very large arrays or lots of computations, in which case youre
better off using C or Fortran).
When you start MATLAB, the command prompt >> appears. You will tell
MATLAB what to do by typing commands at the prompt.
Creating matrices
The basic data element in MATLAB is a matrix. A scalar in MATLAB is a 1x1
matrix,
and a vector is a 1xn (or nx1) matrix.
Useful matrix functions:
A transpose of matrix A. Also transpose(A).
det(A) determinant of A
eig(A) eigenvalues and eigenvectors
inv(A) inverse of A
svd(A) singular value decomposition
norm(A) matrix or vector norm
find(A) find indices of elements that are nonzero. Can also pass an
expression to this function, e.g. find(A > 1) finds the indices of elements of
A greater than 1.
A few useful math functions:
sqrt(x) square root
sin(x) sine function. See also cos(x), tan(x), etc.
exp(x) exponential
log(x) natural log
log10(x) common log
abs(x) absolute value
mod(x) modulus
factorial(x) factorial function
floor(x) round down. See also ceil(x), round(x).
min(x) minimum elements of an array. See also max(x).
besselj(x) Bessel functions of first kind
Matlab is an interactive system for doing
numerical computations.
A numerical analyst called Cleve Moler wrote the first version of Matlab in the
1970s. It has since evolved into a successful commercial software package.
Matlab relieves you of a lot of the mundane tasks associated with solving
problems numerically. This allows you to spend more time thinking, and
encourages you to experiment.

Вам также может понравиться