fprintf('text %6d %5.3f %2.5e', 3223, pi, 3*pi)
text 3223 3.142 9.42478e+000
help if
IF Conditionally execute statements.
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
The statements are executed if the real part of the expression
has all non-zero elements. The ELSE and ELSEIF parts are optional.
Zero or more ELSEIF parts can be used as well as nested IF's.
The expression is usually of the form expr rop expr where
rop is ==, <, >, <=, >=, or ~=.
Example
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end
See also relop, else, elseif, end, for, while, switch.
Reference page in Help browser
doc if
type factorial
function n = factorial(n)
%FACTORIAL Factorial function.
% FACTORIAL(N) for scalar N, is the product of all the integers from 1 to N,
% i.e. prod(1:N). When N is an N-D matrix, FACTORIAL(N) is the factorial for
% each element of N. Since double precision numbers only have about
% 15 digits, the answer is only accurate for N <= 21. For larger N,
% the answer will have the correct order of magnitude, and is accurate for
% the first 15 digits.
%
% See also PROD.
% Copyright 1998-2004 The MathWorks, Inc.
% $Revision: 1.7.4.6 $ $Date: 2004/06/25 18:52:29 $
N = n(:);
if any(fix(N) ~= N) || any(N < 0) || ~isa(N,'double') || ~isreal(N)
error('MATLAB:factorial:NNegativeInt', ...
'N must be a matrix of non-negative integers.')
end
n(N>170) = 171;
m = max([1; n(:)]);
N = [1 1 cumprod(2:m)];
n(:) = N(n+1);
factorial(-1)
??? Error using ==> factorial
N must be a matrix of non-negative integers.
help ops
Operators and special characters.
Arithmetic operators.
plus - Plus +
uplus - Unary plus +
minus - Minus -
uminus - Unary minus -
mtimes - Matrix multiply *
times - Array multiply .*
mpower - Matrix power ^
power - Array power .^
mldivide - Backslash or left matrix divide \
mrdivide - Slash or right matrix divide /
ldivide - Left array divide .\
rdivide - Right array divide ./
kron - Kronecker tensor product kron
Relational operators.
eq - Equal ==
ne - Not equal ~=
lt - Less than <
gt - Greater than >
le - Less than or equal <=
ge - Greater than or equal >=
Logical operators.
Short-circuit logical AND &&
Short-circuit logical OR ||
and - Element-wise logical AND &
or - Element-wise logical OR |
not - Logical NOT ~
xor - Logical EXCLUSIVE OR
any - True if any element of vector is nonzero
all - True if all elements of vector are nonzero
Special characters.
colon - Colon :
paren - Parentheses and subscripting ( )
paren - Brackets [ ]
paren - Braces and subscripting { }
punct - Function handle creation @
punct - Decimal point .
punct - Structure field access .
punct - Parent directory ..
punct - Continuation ...
punct - Separator ,
punct - Semicolon ;
punct - Comment %
punct - Invoke operating system command !
punct - Assignment =
punct - Quote '
transpose - Transpose .'
ctranspose - Complex conjugate transpose '
horzcat - Horizontal concatenation [,]
vertcat - Vertical concatenation [;]
subsasgn - Subscripted assignment ( ),{ },.
subsref - Subscripted reference ( ),{ },.
subsindex - Subscript index
Bitwise operators.
bitand - Bit-wise AND.
bitcmp - Complement bits.
bitor - Bit-wise OR.
bitmax - Maximum floating point integer.
bitxor - Bit-wise XOR.
bitset - Set bit.
bitget - Get bit.
bitshift - Bit-wise shift.
Set operators.
union - Set union.
unique - Set unique.
intersect - Set intersection.
setdiff - Set difference.
setxor - Set exclusive-or.
ismember - True for set member.
See also arith, relop, slash, function_handle.
help for
FOR Repeat statements a specific number of times.
The general form of a FOR statement is:
FOR variable = expr, statement, ..., statement END
The columns of the expression are stored one at a time in
the variable and then the following statements, up to the
END, are executed. The expression is often of the form X:Y,
in which case its columns are simply scalars. Some examples
(assume N has already been assigned a value).
for R = 1:N
for C = 1:N
A(R,C) = 1/(R+C-1);
end
end
Step S with increments of -0.1
for S = 1.0: -0.1: 0.0, do_some_task(S), end
Set E to the unit N-vectors
for E = eye(N), do_some_task(E), end
Long loops are more memory efficient when the colon expression appears
in the FOR statement since the index vector is never created.
The BREAK statement can be used to terminate the loop prematurely.
See also if, while, switch, break, continue, end, colon.
Reference page in Help browser
doc for
for k=1:10
z(k)= k^2;
end
z
z =
1 4 9 16 25 36 49 64 81 100
for k=1:10
z(k)= k^2
end
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
z =
1 4 9 16 25 36 49 64 81 100
clear z
for k=1:10
z(k)= k^2
end
z =
1
z =
1 4
z =
1 4 9
z =
1 4 9 16
z =
1 4 9 16 25
z =
1 4 9 16 25 36
z =
1 4 9 16 25 36 49
z =
1 4 9 16 25 36 49 64
z =
1 4 9 16 25 36 49 64 81
z =
1 4 9 16 25 36 49 64 81 100
zz= [1:10].^2
zz =
1 4 9 16 25 36 49 64 81 100
help while
WHILE Repeat statements an indefinite number of times.
The general form of a WHILE statement is:
WHILE expression
statements
END
The statements are executed while the real part of the expression
has all non-zero elements. The expression is usually the result of
expr rop expr where rop is ==, <, >, <=, >=, or ~=.
The BREAK statement can be used to terminate the loop prematurely.
For example (assuming A already defined):
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
See also for, if, switch, break, continue, end.
Reference page in Help browser
doc while
x= 10;
while x>3
disp(x), x=x-3;
end
10
7
4
% infinite loops, while(1); 1= true; either "break", or ctrl c
cd matlab\velocity\
open velocity2.m
velocity(0.35, 0, 12, 0, 68.1, .25)
Warning: Size vector should be a row vector with integer elements.
> In velocity at 18
The velocity is 50.8012 m/s
ans =
50.8012
velocity2(0.35, 0, 12, 0, 68.1, .25)
ans =
50.8348
diary off