ls
Challenge-1.nb challenge1.tex lect-fpa.nb lect-fpa.tex
HW1-sol.pdf diary lect-fpa.org my_pi_down.m
HW1-sol.tex exercises-1.pdf lect-fpa.org~ my_pi_up.m
challenge1.pdf exercises-1.tex lect-fpa.pdf
help ls
ls List directory.
ls displays the results of the 'ls' command on UNIX. On UNIX,
ls returns a character row vector of filenames separated
by tab and space characters. On Windows, ls returns an m-by-n
character array of filenames, where m is the number of filenames
and n is the number of characters in the longest filename found.
Filenames shorter than n characters are padded with space characters.
You can pass any flags to ls as well that your operating system supports.
See also dir, mkdir, rmdir, fileattrib, copyfile, movefile, delete.
Reference page in Help browser
doc ls
clr
{Undefined function or variable 'clr'.
}
clf
cls
{Undefined function or variable 'cls'.
}
clear
clc
pwd
ans =
/Users/pauldj/works/courses/LSC/LSC-17/FPA
eye(4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
M = floor(rand(4,4))
M =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
M = floor(10*rand(4,4))
M =
4 6 6 6
9 0 7 1
7 8 7 7
9 9 3 0
diag(M)
ans =
4
0
7
0
diag([1 2 3 4])
ans =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
diag(diag([1 2 3 4]))
ans =
1
2
3
4
diag([1 2 3 4],-1)
ans =
0 0 0 0 0
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
diag([1 2 3 4],3)
ans =
0 0 0 1 0 0 0
0 0 0 0 2 0 0
0 0 0 0 0 3 0
0 0 0 0 0 0 4
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
diag(diag([1 2 3 4],3),-2)
ans =
0
0
0
0
0
help diag
diag Diagonal matrices and diagonals of a matrix.
diag(V,K) when V is a vector with N components is a square matrix
of order N+ABS(K) with the elements of V on the K-th diagonal. K = 0
is the main diagonal, K > 0 is above the main diagonal and K < 0
is below the main diagonal.
diag(V) is the same as diag(V,0) and puts V on the main diagonal.
diag(X,K) when X is a matrix is a column vector formed from
the elements of the K-th diagonal of X.
diag(X) is the main diagonal of X. diag(diag(X)) is a diagonal matrix.
Example
m = 5;
diag(-m:m) + diag(ones(2*m,1),1) + diag(ones(2*m,1),-1)
produces a tridiagonal matrix of order 2*m+1.
See also spdiags, triu, tril, blkdiag.
Reference page in Help browser
doc diag
M
M =
4 6 6 6
9 0 7 1
7 8 7 7
9 9 3 0
eig(M)
ans =
22.4482 + 0.0000i
-5.3918 + 3.5713i
-5.3918 - 3.5713i
-0.6646 + 0.0000i
[E] = eig(M)
E =
22.4482 + 0.0000i
-5.3918 + 3.5713i
-5.3918 - 3.5713i
-0.6646 + 0.0000i
[E,V] = eig(M)
E =
Columns 1 through 3
0.4837 + 0.0000i -0.1977 + 0.2029i -0.1977 - 0.2029i
0.4110 + 0.0000i 0.6566 + 0.0000i 0.6566 + 0.0000i
0.6329 + 0.0000i -0.2154 + 0.1590i -0.2154 - 0.1590i
0.4433 + 0.0000i -0.2524 - 0.5943i -0.2524 + 0.5943i
Column 4
-0.4503 + 0.0000i
0.2810 + 0.0000i
0.6328 + 0.0000i
-0.5637 + 0.0000i
V =
Columns 1 through 3
22.4482 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -5.3918 + 3.5713i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -5.3918 - 3.5713i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
Column 4
0.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
-0.6646 + 0.0000i
[V,E] = eig(M)
V =
Columns 1 through 3
0.4837 + 0.0000i -0.1977 + 0.2029i -0.1977 - 0.2029i
0.4110 + 0.0000i 0.6566 + 0.0000i 0.6566 + 0.0000i
0.6329 + 0.0000i -0.2154 + 0.1590i -0.2154 - 0.1590i
0.4433 + 0.0000i -0.2524 - 0.5943i -0.2524 + 0.5943i
Column 4
-0.4503 + 0.0000i
0.2810 + 0.0000i
0.6328 + 0.0000i
-0.5637 + 0.0000i
E =
Columns 1 through 3
22.4482 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -5.3918 + 3.5713i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -5.3918 - 3.5713i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
Column 4
0.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
-0.6646 + 0.0000i
help eig
eig Eigenvalues and eigenvectors.
E = eig(A) produces a column vector E containing the eigenvalues of
a square matrix A.
[V,D] = eig(A) produces a diagonal matrix D of eigenvalues and
a full matrix V whose columns are the corresponding eigenvectors
so that A*V = V*D.
[V,D,W] = eig(A) also produces a full matrix W whose columns are the
corresponding left eigenvectors so that W'*A = D*W'.
[...] = eig(A,'nobalance') performs the computation with balancing
disabled, which sometimes gives more accurate results for certain
problems with unusual scaling. If A is symmetric, eig(A,'nobalance')
is ignored since A is already balanced.
[...] = eig(A,'balance') is the same as eig(A).
E = eig(A,B) produces a column vector E containing the generalized
eigenvalues of square matrices A and B.
[V,D] = eig(A,B) produces a diagonal matrix D of generalized
eigenvalues and a full matrix V whose columns are the corresponding
eigenvectors so that A*V = B*V*D.
[V,D,W] = eig(A,B) also produces a full matrix W whose columns are the
corresponding left eigenvectors so that W'*A = D*W'*B.
[...] = eig(A,B,'chol') is the same as eig(A,B) for symmetric A and
symmetric positive definite B. It computes the generalized eigenvalues
of A and B using the Cholesky factorization of B.
[...] = eig(A,B,'qz') ignores the symmetry of A and B and uses the QZ
algorithm. In general, the two algorithms return the same result,
however using the QZ algorithm may be more stable for certain problems.
The flag is ignored when A or B are not symmetric.
[...] = eig(...,'vector') returns eigenvalues in a column vector
instead of a diagonal matrix.
[...] = eig(...,'matrix') returns eigenvalues in a diagonal matrix
instead of a column vector.
See also condeig, eigs, ordeig.
Reference page in Help browser
doc eig
help lu
lu lu factorization.
[L,U] = lu(A) stores an upper triangular matrix in U and a
"psychologically lower triangular matrix" (i.e. a product of lower
triangular and permutation matrices) in L, so that A = L*U. A can be
rectangular.
[L,U,P] = lu(A) returns unit lower triangular matrix L, upper
triangular matrix U, and permutation matrix P so that P*A = L*U.
[L,U,p] = lu(A,'vector') returns the permutation information as a
vector instead of a matrix. That is, p is a row vector such that
A(p,:) = L*U. Similarly, [L,U,P] = lu(A,'matrix') returns a
permutation matrix P. This is the default behavior.
Y = lu(A) returns the output from LAPACK'S DGETRF or ZGETRF routine if
A is full. If A is sparse, Y contains the strict lower triangle of L
embedded in the same matrix as the upper triangle of U. In both full
and sparse cases, the permutation information is lost.
[L,U,P,Q] = lu(A) returns unit lower triangular matrix L, upper
triangular matrix U, a permutation matrix P and a column reordering
matrix Q so that P*A*Q = L*U for sparse non-empty A. This uses UMFPACK
and is significantly more time and memory efficient than the other
syntaxes, even when used with COLAMD.
[L,U,p,q] = lu(A,'vector') returns two row vectors p and q so that
A(p,q) = L*U. Using 'matrix' in place of 'vector' returns permutation
matrices.
[L,U,P,Q,R] = lu(A) returns unit lower triangular matrix L, upper
triangular matrix U, permutation matrices P and Q, and a diagonal
scaling matrix R so that P*(R\A)*Q = L*U for sparse non-empty A.
This uses UMFPACK as well. Typically, but not always, the row-scaling
leads to a sparser and more stable factorization. Note that this
factorization is the same as that used by sparse MLDIVIDE when
UMFPACK is used.
[L,U,p,q,R] = lu(A,'vector') returns the permutation information in two
row vectors p and q such that R(:,p)\A(:,q) = L*U. Using 'matrix'
in place of 'vector' returns permutation matrices.
[L,U,P] = lu(A,THRESH) controls pivoting in sparse matrices, where
THRESH is a pivot threshold in [0,1]. Pivoting occurs when the
diagonal entry in a column has magnitude less than THRESH times the
magnitude of any sub-diagonal entry in that column. THRESH = 0 forces
diagonal pivoting. THRESH = 1 is the default.
[L,U,P,Q,R] = lu(A,THRESH) controls pivoting in UMFPACK. THRESH is a
one or two element vector which defaults to [0.1 0.001]. If UMFPACK
selects its unsymmetric pivoting strategy, THRESH(2) is not used. It
uses its symmetric pivoting strategy if A is square with a mostly
symmetric nonzero structure and a mostly nonzero diagonal. For its
unsymmetric strategy, the sparsest row i which satisfies the criterion
A(i,j) >= THRESH(1) * max(abs(A(j:m,j))) is selected. A value of 1.0
results in conventional partial pivoting. Entries in L have absolute
value of 1/THRESH(1) or less. For its symmetric strategy, the diagonal
is selected using the same test but with THRESH(2) instead. If the
diagonal entry fails this test, a pivot entry below the diagonal is
selected, using THRESH(1). In this case, L has entries with absolute
value 1/min(THRESH) or less. Smaller values of THRESH(1) and THRESH(2)
tend to lead to sparser lu factors, but the solution can become
inaccurate. Larger values can lead to a more accurate solution (but
not always), and usually an increase in the total work and memory
usage.
[L,U,p] = lu(A,THRESH,'vector') and [L,U,p,q,R] = lu(A,THRESH,'vector')
are also valid for sparse matrices and return permutation vectors.
Using 'matrix' in place of 'vector' returns permutation matrices.
See also chol, ilu, qr.
Reference page in Help browser
doc lu
A = [ 1 2; 3 4 ];
B = [ 1 -1; 1 1 ];
A
A =
1 2
3 4
B
B =
1 -1
1 1
A*B
ans =
3 1
7 1
B*A
ans =
-2 -2
4 6
2.^[2 3 4]
ans =
4 8 16
2^[2 3 4]
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
}
n = 3; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; X1-XI1
ans =
1.0e-14 *
0.3553 0.5329 0.3553
0 -0.0222 0
0 -0.0444 0
n = 5; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; X1-XI1
ans =
1.0e-15 *
-0.1110 0.4441 -0.3331 0.3331 0.4441
-0.1665 -0.4441 0.2220 -0.4441 -0.4441
0 0.4441 0.5551 -0.1110 0
0.0278 0 0 0 -0.1110
0.4441 0.8882 0.2220 0.5551 0.4441
n = 5; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; norm(X1-XI1)
ans =
1.2514e-15
n = 50; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; norm(X1-XI1)
ans =
4.2381e-14
n = 150; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; norm(X1-XI1)
ans =
1.7214e-12
n = 500; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; norm(X1-XI1)
ans =
1.4961e-12
n = 1500; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; norm(X1-XI1)
ans =
1.0129e-10
n = 2000; A = rand(n,n); B = rand(n,n); X1 = A\B; XI1 = inv(A)*B; norm(X1-XI1)
ans =
5.6759e-11
n = 2000; A = rand(n,n); B = rand(n,n); tic; X1 = A\B; toc
Elapsed time is 0.168936 seconds.
n = 2000; A = rand(n,n); B = rand(n,n); tic; X1 = A\B; toc
Elapsed time is 0.164254 seconds.
n = 2000; A = rand(n,n); B = rand(n,n); tic; X1 = A\B; toc
Elapsed time is 0.163834 seconds.
n = 2000; A = rand(n,n); B = rand(n,n); tic; XI1 = inv(A)*B; toc
Elapsed time is 0.232144 seconds.
n = 2000; A = rand(n,n); B = rand(n,n); tic; XI1 = inv(A)*B; toc
Elapsed time is 0.233072 seconds.
n = 2000; A = rand(n,n); B = rand(n,n); tic; XI1 = inv(A)*B; toc
Elapsed time is 0.231721 seconds.
n = 4000; A = rand(n,n); B = rand(n,n); tic; X1 = A\B; toc
Elapsed time is 1.314820 seconds.
n = 4000; A = rand(n,n); B = rand(n,n); tic; XI1 = inv(A)*B; toc
Elapsed time is 1.927427 seconds.
n = 5000; A = rand(n,n); B = rand(n,n); tic; X1 = A\B; toc
Elapsed time is 2.607146 seconds.
n = 5000; A = rand(n,n); B = rand(n,n); tic; XI1 = inv(A)*B; toc
Elapsed time is 3.831036 seconds.
n = 5000; A = rand(n,n); B = rand(n,n); tic; X1 = A\B; toc
Elapsed time is 2.489635 seconds.
help linsystem
linsystem not found.
Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.
doc linsyst
help linsolve
linsolve Solve linear system A*X=B.
X = linsolve(A,B) solves the linear system A*X=B using
LU factorization with partial pivoting when A is square,
and QR factorization with column pivoting otherwise.
Warning is given if A is ill conditioned for square matrices
and rank deficient for rectangular matrices.
[X, R] = linsolve(A,B) suppresses these warnings and returns R
the reciprocal of the condition number of A for square matrices,
and the rank of A if A is rectangular.
X = linsolve(A,B,OPTS) solves the linear system A*X=B,
with an appropriate solver determined by the properties of
the matrix A as described by the structure OPTS. The fields of OPTS
must contain logicals. All field values are defaulted to false.
No test is performed to verify whether A possesses such properties.
[X, R] = linsolve(A,B,OPTS) suppresses these warnings and returns R,
the reciprocal of the condition number of A, or the rank of A (depending
on OPTS). See table below for more information.
Below is the list of all possible field names and
their corresponding matrix properties.
Field Name : Matrix Property
------------------------------------------------
LT : Lower Triangular
UT : Upper Triangular
UHESS : Upper Hessenberg
SYM : Real Symmetric or Complex Hermitian
POSDEF : Positive Definite
RECT : General Rectangular
TRANSA : (Conjugate) Transpose of A
Here is a table containing all possible combinations of options:
LT UT UHESS SYM POSDEF RECT TRANSA Output R
----------------------------------------- ----------------
T F F F F T/F T/F condition number
F T F F F T/F T/F condition number
F F T F F F T/F condition number
F F F T T/F F T/F condition number
F F F F F T/F T/F rank
Example:
A = triu(rand(5,3)); x = [1 1 1 0 0]'; b = A'*x;
y1 = (A')\b
opts.UT = true; opts.TRANSA = true;
y2 = linsolve(A,b,opts)
See also mldivide, slash.
Reference page in Help browser
doc linsolve
doc linsolve
C = reshape(1:24, 6, 4)
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 10 16 22
5 11 17 23
6 12 18 24
C( [2 3], 4:6 )
{Index exceeds matrix dimensions.
}
C( [2 3], 4:4 )
ans =
20
21
C( 4:6, [2 3] )
ans =
10 16
11 17
12 18
C
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 10 16 22
5 11 17 23
6 12 18 24
C( 4:6, [2 3] ) = ones( 3, 2 )
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 1 1 22
5 1 1 23
6 1 1 24
C( 4:6, [2 3] ) = [
C( 4:6, [2 3] ) = [ -1 2; 3 -4; 5 6 ]
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 -1 2 22
5 3 -4 23
6 5 6 24
C( 4:6, [2 3] ) = pi
C =
1.0000 7.0000 13.0000 19.0000
2.0000 8.0000 14.0000 20.0000
3.0000 9.0000 15.0000 21.0000
4.0000 3.1416 3.1416 22.0000
5.0000 3.1416 3.1416 23.0000
6.0000 3.1416 3.1416 24.0000
C = reshape(1:24, 6, 4)
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 10 16 22
5 11 17 23
6 12 18 24
whos('C')
Name Size Bytes Class Attributes
C 6x4 192 double
C( 4:6, [2 3] ) = pi
C =
1.0000 7.0000 13.0000 19.0000
2.0000 8.0000 14.0000 20.0000
3.0000 9.0000 15.0000 21.0000
4.0000 3.1416 3.1416 22.0000
5.0000 3.1416 3.1416 23.0000
6.0000 3.1416 3.1416 24.0000
whos('C')
Name Size Bytes Class Attributes
C 6x4 192 double
C = floor(C)
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 3 3 22
5 3 3 23
6 3 3 24
whos('C')
Name Size Bytes Class Attributes
C 6x4 192 double
D = C > 5
D =
0 1 1 1
0 1 1 1
0 1 1 1
0 0 0 1
0 0 0 1
1 0 0 1
whos('D')
Name Size Bytes Class Attributes
D 6x4 24 logical
D(3,3) = pi
D =
0 1 1 1
0 1 1 1
0 1 1 1
0 0 0 1
0 0 0 1
1 0 0 1
whos('D')
Name Size Bytes Class Attributes
D 6x4 24 logical
D(3,3) = -2
D =
0 1 1 1
0 1 1 1
0 1 1 1
0 0 0 1
0 0 0 1
1 0 0 1
D(3,3) += 3
D(3,3) += 3
|
{Error: The expression to the left of the equals sign is not a valid target
for an assignment.
}
D = D*3
D =
0 3 3 3
0 3 3 3
0 3 3 3
0 0 0 3
0 0 0 3
3 0 0 3
whos('D')
Name Size Bytes Class Attributes
D 6x4 192 double
C
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 3 3 22
5 3 3 23
6 3 3 24
D = C(:)
D =
1
2
3
4
5
6
7
8
9
3
3
3
13
14
15
3
3
3
19
20
21
22
23
24
size(D)
ans =
24 1
D( 4:12 ) = []
D =
1
2
3
13
14
15
3
3
3
19
20
21
22
23
24
size(D( 5:8 ))
ans =
4 1
size(D( 8:-1:5 ))
ans =
4 1
size(D( 8:5 ))
ans =
0 1
C
C =
1 7 13 19
2 8 14 20
3 9 15 21
4 3 3 22
5 3 3 23
6 3 3 24
size( C(3:4, :) )
ans =
2 4
size( C(4:3, :) )
ans =
0 4
D = C(4:3, :)
D =
Empty matrix: 0-by-4
D * rand(4, 10)
ans =
Empty matrix: 0-by-10
E = D * rand(4, 10)
E =
Empty matrix: 0-by-10
size(D)
ans =
0 4
D' * E
ans =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
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
edit traverse
size( rand(5,5) ) > 4
ans =
1 1
travers
{Undefined function or variable 'travers'.
}
traverse
ans =
11
ans =
5
ans =
3
ans =
0
ans =
11
ans =
9
ans =
1
ans =
2
traverse
M =
9 9 10 8 1 11 5 5
4 1 0 10 1 11 6 11
0 4 11 4 10 11 4 8
4 0 7 11 6 0 3 6
8 3 6 8 11 2 11 0
6 3 6 0 2 5 1 7
1 11 10 8 7 11 0 9
11 11 9 2 10 5 9 5
ans =
9
ans =
1
ans =
11
ans =
11
ans =
11
ans =
5
ans =
0
ans =
5
traverse
M =
1 4 7 5 5 1 10 5
6 4 6 8 6 6 9 4
3 0 7 11 10 8 5 9
7 1 8 3 2 11 4 8
11 10 3 2 9 7 6 10
4 10 6 1 0 4 7 4
3 9 11 5 11 6 7 10
7 11 9 6 0 10 10 1
ans =
1 4
6 4
ans =
7 11
8 3
ans =
9 7
0 4
ans =
7 10
10 1
traverse
M =
9 10 6 3 2 10 4 3 4
10 1 11 1 2 1 5 2 5
9 7 11 4 4 1 0 7 3
1 3 6 8 7 9 10 9 1
5 10 7 7 4 9 1 9 9
4 6 8 11 5 9 3 4 4
0 5 0 7 2 10 3 8 7
9 8 0 8 5 7 5 7 3
0 8 5 6 10 2 2 5 11
ans =
9 10
10 1
ans =
11 4
6 8
ans =
4 9
5 9
ans =
3 8
5 7
{Index exceeds matrix dimensions.
Error in traverse (line 7)
M(1:b, 1:b),
}
traverse
M =
2 10 9 0 11 3 9 9 1
5 5 3 5 6 4 8 1 7
10 2 8 11 8 1 8 2 11
5 0 9 6 0 9 2 10 2
3 5 4 8 6 5 6 11 9
0 7 6 6 10 10 9 10 5
0 6 11 3 7 6 10 4 2
0 11 8 7 10 2 0 2 3
6 11 6 5 6 10 0 5 9
ans =
2 10
5 5
ans =
8 11
9 6
ans =
6 5
10 10
ans =
10 4
0 2
ans =
9
traverse
M =
Columns 1 through 12
4 8 4 10 8 11 0 2 4 2 1 5
4 6 10 3 7 6 1 8 0 4 4 6
1 5 0 0 0 10 3 8 7 9 10 9
4 11 2 10 6 5 6 6 3 8 7 3
5 3 1 9 5 4 1 2 8 4 11 7
11 8 8 4 6 1 7 2 0 9 6 9
1 5 6 5 4 7 0 11 4 5 1 8
4 1 10 5 2 3 6 0 4 8 11 2
1 0 8 11 2 9 4 6 4 3 6 4
0 3 8 2 6 11 11 7 1 1 3 2
0 8 11 10 2 10 5 0 0 11 8 8
6 6 8 7 1 2 1 1 10 6 3 2
6 9 6 6 3 5 11 5 0 3 2 9
Column 13
0
1
5
0
5
10
8
8
3
3
4
7
3
ans =
4 8 4 10
4 6 10 3
1 5 0 0
4 11 2 10
ans =
5 4 1 2
6 1 7 2
4 7 0 11
2 3 6 0
ans =
4 3 6 4
1 1 3 2
0 11 8 8
10 6 3 2
ans =
3
traverse
M =
7 4 10 8 3 5 2 10 1 2 0
3 5 5 10 1 10 4 7 4 7 10
4 8 7 10 7 2 6 2 0 8 11
1 7 11 1 10 5 10 6 6 3 6
1 1 0 9 7 4 3 4 11 10 3
9 5 4 11 9 4 11 1 3 3 11
8 8 11 1 11 11 7 3 1 5 6
8 11 11 8 10 3 6 11 10 10 0
1 0 1 7 11 5 9 0 11 2 1
8 6 2 6 0 0 4 11 4 7 7
11 3 0 4 6 0 9 1 1 8 0
ans =
7 4 10 8
3 5 5 10
4 8 7 10
1 7 11 1
ans =
7 4 3 4
9 4 11 1
11 11 7 3
10 3 6 11
ans =
11 2 1
4 7 7
1 8 0
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 parfor, if, while, switch, break, continue, end, colon.
Reference page in Help browser
doc for
for i = 1:5, i^2, end
ans =
1
ans =
4
ans =
9
ans =
16
ans =
25
for i = [-1 -2 -3 -4], i^2, end
ans =
1
ans =
4
ans =
9
ans =
16
for i = linspace(1,2,10), i^2, end
ans =
1
ans =
1.2346
ans =
1.4938
ans =
1.7778
ans =
2.0864
ans =
2.4198
ans =
2.7778
ans =
3.1605
ans =
3.5679
ans =
4
edit crazy
help crazy
No help found for crazy.m.
[3 [4;5] 5]
{Error using horzcat
Dimensions of matrices being concatenated are not consistent.
}
[3 [4;5]' 5]
ans =
3 4 5 5
[a,b,c] = lu(rand(5,5))
a =
1.0000 0 0 0 0
0.9954 1.0000 0 0 0
0.9228 0.5234 1.0000 0 0
0.7360 0.5825 0.4730 1.0000 0
0.7338 0.8303 -0.4071 -0.9282 1.0000
b =
0.8384 0.7477 0.0828 0.1133 0.5820
0 -0.6000 0.3959 0.7279 0.1489
0 0 0.6014 0.0281 -0.5508
0 0 0 0.4231 -0.0406
0 0 0 0 -0.4486
c =
0 0 0 1 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
1 0 0 0 0
[a,b,c,d,e] = lu(rand(5,5))
{Error using lu
Too many output arguments.
}
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
[ zeros(n,n) ]
ans =
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
clear('n')
[ zeros(n,n) ]
{Undefined function or variable 'n'.
}
n = 6;
[ zeros(n,n), ones(n,n);
2*ones(n,n/2), 3*ones(n,n), 4*ones(n,n/2) ]
ans =
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
[zeros(n,n) , , ones(n,n);
[zeros(n,n) , , ones(n,n);
|
{Error: Expression or statement is incorrect--possibly unbalanced (, {, or
[.
}
[zeros(n,n) , [] , ones(n,n);
2*ones(n,n/2), 3*ones(n,n), 4*ones(n,n/2) ];
2*ones(n,n/2), 3*ones(n,n), 4*ones(n,n/2) ]
2*ones(n,n/2), 3*ones(n,n), 4*ones(n,n/2) ]
|
{Error: Unbalanced or unexpected parenthesis or bracket.
}
[zeros(n,n) , [] , ones(n,n);
2*ones(n,n/2), 3*ones(n,n), 4*ones(n,n/2) ]
ans =
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
2 2 2 3 3 3 3 3 3 4 4 4
[zeros(n,n) , [] , 3*ones(n,n);
ones(n,n/2) , 2*ones(n,n), 4*ones(n,n/2) ]'
ans =
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 2 2 2 2 2 2
0 0 0 0 0 0 2 2 2 2 2 2
0 0 0 0 0 0 2 2 2 2 2 2
3 3 3 3 3 3 2 2 2 2 2 2
3 3 3 3 3 3 2 2 2 2 2 2
3 3 3 3 3 3 2 2 2 2 2 2
3 3 3 3 3 3 4 4 4 4 4 4
3 3 3 3 3 3 4 4 4 4 4 4
3 3 3 3 3 3 4 4 4 4 4 4
[[ zeros(n,n);
3*ones(n,n); ], [ ones(n/2,n/2);
2*ones(n,n/2) ;
4*ones(n/2,n/2); ]]
ans =
0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 2 2 2
0 0 0 0 0 0 2 2 2
0 0 0 0 0 0 2 2 2
3 3 3 3 3 3 2 2 2
3 3 3 3 3 3 2 2 2
3 3 3 3 3 3 2 2 2
3 3 3 3 3 3 4 4 4
3 3 3 3 3 3 4 4 4
3 3 3 3 3 3 4 4 4