A = 3*randn(5,5)
A =
1.6130 -3.9231 -4.0497 -0.6149 2.0145
5.5017 -1.3008 9.1048 -0.3724 -3.6225
-6.7765 1.0279 2.1762 4.4691 2.1517
2.5865 10.7352 -0.1892 4.2271 4.8907
0.9563 8.3083 2.1442 4.2516 1.4667
det(A)
ans =
-5.6738e+03
inv(A)
ans =
0.1469 0.0505 -0.0659 -0.0321 0.1269
-0.1501 -0.0458 -0.0540 0.0604 -0.0290
-0.1115 0.1043 0.0841 0.1635 -0.2580
0.3294 -0.0019 0.0340 -0.3221 0.5673
-0.0371 0.0795 0.1272 0.3737 -0.5037
IN = inv(A);
IN*A
ans =
1.0000 0 0.0000 0.0000 0.0000
0.0000 1.0000 0.0000 -0.0000 -0.0000
0.0000 0 1.0000 -0.0000 -0.0000
-0.0000 0.0000 0 1.0000 0.0000
0.0000 -0.0000 0 -0.0000 1.0000
A*IN
ans =
1.0000 0.0000 0 -0.0000 0
-0.0000 1.0000 -0.0000 0 0.0000
-0.0000 -0.0000 1.0000 0.0000 0.0000
0.0000 0.0000 0.0000 1.0000 0.0000
0.0000 -0.0000 0.0000 -0.0000 1.0000
clear
A = [1 0 4;-2 2 1;0 0 3]
A =
1 0 4
-2 2 1
0 0 3
lambda = eig(A)
lambda =
2
1
3
[V,D] = eig(A)
V =
0 0.4472 0.5345
1.0000 0.8944 -0.8018
0 0 0.2673
D =
2 0 0
0 1 0
0 0 3
2/sqrt(5)
ans =
0.8944
1/sqrt(5)
ans =
0.4472
v2 = V(:,2)
v2 =
0.4472
0.8944
0
norm(v2)
ans =
1
A*v2
ans =
0.4472
0.8944
0
v2
v2 =
0.4472
0.8944
0
A*V
ans =
0 0.4472 1.6036
2.0000 0.8944 -2.4054
0 0 0.8018
V*D
ans =
0 0.4472 1.6036
2.0000 0.8944 -2.4054
0 0 0.8018
A2 = A^2;
eig(A2)
ans =
4
1
9
A3 = A^3;
eig(A3)
ans =
8
1
27
eig(inv(A))
ans =
0.5000
1.0000
0.3333
format rat
eig(inv(A))
ans =
1/2
1
1/3
format short
clear
A = 4*randi([0,10],10)
A =
32 36 20 20 40 12 12 4 24 36
32 28 16 40 24 8 32 24 8 20
16 12 28 12 4 8 32 20 28 40
28 40 28 24 4 24 16 0 28 0
4 0 32 8 8 20 24 12 32 16
28 16 12 32 36 12 0 4 16 4
0 16 28 8 8 36 0 32 0 40
12 32 28 20 32 24 20 12 8 0
0 32 4 28 8 24 32 20 40 32
4 8 4 36 40 40 40 4 4 32
issymetric(A)
{Unrecognized function or variable 'issymetric'.
}
issymmetric(A)
ans =
logical
0
B = A*A'
B =
Columns 1 through 7
6896 5600 4576 4688 3168 4480 3616
5600 6368 4464 4448 2944 4080 3264
4576 4464 5216 3504 3792 2288 3632
4688 4448 3504 5376 2992 3408 2512
3168 2944 3792 2992 3568 1904 2768
4480 4080 2288 3408 1904 3936 1856
3616 3264 3632 2512 2768 1856 5088
4544 4480 3024 4128 2720 3440 2960
4976 4928 4880 4304 3696 2880 3696
5040 5184 3936 3280 3200 3568 3696
Columns 8 through 10
4544 4976 5040
4480 4928 5184
3024 4880 3936
4128 4304 3280
2720 3696 3200
3440 2880 3568
2960 3696 3696
4560 3728 4256
3728 6512 5104
4256 5104 7248
issymmetric(B)
ans =
logical
1
istril(A)
ans =
logical
0
istril(B)
ans =
logical
0
%istriu %isdiag
A
A =
32 36 20 20 40 12 12 4 24 36
32 28 16 40 24 8 32 24 8 20
16 12 28 12 4 8 32 20 28 40
28 40 28 24 4 24 16 0 28 0
4 0 32 8 8 20 24 12 32 16
28 16 12 32 36 12 0 4 16 4
0 16 28 8 8 36 0 32 0 40
12 32 28 20 32 24 20 12 8 0
0 32 4 28 8 24 32 20 40 32
4 8 4 36 40 40 40 4 4 32
U = triu(A)
U =
32 36 20 20 40 12 12 4 24 36
0 28 16 40 24 8 32 24 8 20
0 0 28 12 4 8 32 20 28 40
0 0 0 24 4 24 16 0 28 0
0 0 0 0 8 20 24 12 32 16
0 0 0 0 0 12 0 4 16 4
0 0 0 0 0 0 0 32 0 40
0 0 0 0 0 0 0 12 8 0
0 0 0 0 0 0 0 0 40 32
0 0 0 0 0 0 0 0 0 32
istriu(U)
ans =
logical
1
isdiag(U)
ans =
logical
0
D = diag(diag(A))
D =
32 0 0 0 0 0 0 0 0 0
0 28 0 0 0 0 0 0 0 0
0 0 28 0 0 0 0 0 0 0
0 0 0 24 0 0 0 0 0 0
0 0 0 0 8 0 0 0 0 0
0 0 0 0 0 12 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 12 0 0
0 0 0 0 0 0 0 0 40 0
0 0 0 0 0 0 0 0 0 32
D = triu(tril(A))
D =
32 0 0 0 0 0 0 0 0 0
0 28 0 0 0 0 0 0 0 0
0 0 28 0 0 0 0 0 0 0
0 0 0 24 0 0 0 0 0 0
0 0 0 0 8 0 0 0 0 0
0 0 0 0 0 12 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 12 0 0
0 0 0 0 0 0 0 0 40 0
0 0 0 0 0 0 0 0 0 32
isdiag(D)
ans =
logical
1
U = triu(A,1)
U =
0 36 20 20 40 12 12 4 24 36
0 0 16 40 24 8 32 24 8 20
0 0 0 12 4 8 32 20 28 40
0 0 0 0 4 24 16 0 28 0
0 0 0 0 0 20 24 12 32 16
0 0 0 0 0 0 0 4 16 4
0 0 0 0 0 0 0 32 0 40
0 0 0 0 0 0 0 0 8 0
0 0 0 0 0 0 0 0 0 32
0 0 0 0 0 0 0 0 0 0
U = triu(A,-3)
U =
32 36 20 20 40 12 12 4 24 36
32 28 16 40 24 8 32 24 8 20
16 12 28 12 4 8 32 20 28 40
28 40 28 24 4 24 16 0 28 0
0 0 32 8 8 20 24 12 32 16
0 0 12 32 36 12 0 4 16 4
0 0 0 8 8 36 0 32 0 40
0 0 0 0 32 24 20 12 8 0
0 0 0 0 0 24 32 20 40 32
0 0 0 0 0 0 40 4 4 32
clear
A = [ones(5,1) 2*ones(5,1) 3*ones(5,1) 4*ones(5,1) 5*ones(5,1)]
A =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
DD = spdiags(A(:,1:3), 0:2, 5)
DD =
(1,1) 1
DD = spdiags(A(:,1:3), [0,1,2], 5)
DD =
(1,1) 1
help spdiags
spdiags Sparse matrix formed from diagonals.
spdiags, which generalizes the function "diag", deals with three
matrices, in various combinations, as both input and output.
[B,d] = spdiags(A) extracts all nonzero diagonals from the m-by-n
matrix A. B is a min(m,n)-by-p matrix whose columns are the p
nonzero diagonals of A. d is a vector of length p whose integer
components specify the diagonals in A.
B = spdiags(A,d) extracts the diagonals specified by d.
A = spdiags(B,d,A) replaces the diagonals of A specified by d with
the columns of B. The output is sparse.
A = spdiags(B,d,m,n) creates an m-by-n sparse matrix from the
columns of B and places them along the diagonals specified by d.
Roughly, A, B and d are related by
for k = 1:p
B(:,k) = diag(A,d(k))
end
Example: These commands generate a sparse tridiagonal representation
of the classic second difference operator on n points.
e = ones(n,1);
A = spdiags([e -2*e e], -1:1, n, n)
Some elements of B, corresponding to positions "outside" of A, are
not actually used. They are not referenced when B is an input and
are set to zero when B is an output. If a column of B is longer than
the diagonal it's representing, elements of super-diagonals of A
correspond to the lower part of the column of B, while elements of
sub-diagonals of A correspond to the upper part of the column of B.
Example: This uses the top of the first column of B for the second
sub-diagonal and the bottom of the third column of B for the first
super-diagonal.
B = repmat((1:n)',1,3);
S = spdiags(B,[-2 0 1],n,n);
See also diag, speye.
Documentation for spdiags
Other functions named spdiags
DD = spdiags(A(:,1:3), [0,1,2], 5,5)
DD =
(1,1) 1
(1,2) 2
(2,2) 1
(1,3) 3
(2,3) 2
(3,3) 1
(2,4) 3
(3,4) 2
(4,4) 1
(3,5) 3
(4,5) 2
(5,5) 1
full(DD)
ans =
1 2 3 0 0
0 1 2 3 0
0 0 1 2 3
0 0 0 1 2
0 0 0 0 1
F = full(DD);
whos
Name Size Bytes Class Attributes
A 5x5 200 double
DD 5x5 240 double sparse
F 5x5 200 double
ans 5x5 200 double
DD = spdiags(A(:,1:3), [0,1,2], 25,25)
{Error using spdiags
For the syntax spdiags(B,d,m,n), the size of B must be min(m,n)-by-length(d).
}
x = ones(25,1);
y = 2*ones(25,1);
z = 3*ones(25,1);
DD = spdiags([x,y,z], [0,1,2], 25,25)
DD =
(1,1) 1
(1,2) 2
(2,2) 1
(1,3) 3
(2,3) 2
(3,3) 1
(2,4) 3
(3,4) 2
(4,4) 1
(3,5) 3
(4,5) 2
(5,5) 1
(4,6) 3
(5,6) 2
(6,6) 1
(5,7) 3
(6,7) 2
(7,7) 1
(6,8) 3
(7,8) 2
(8,8) 1
(7,9) 3
(8,9) 2
(9,9) 1
(8,10) 3
(9,10) 2
(10,10) 1
(9,11) 3
(10,11) 2
(11,11) 1
(10,12) 3
(11,12) 2
(12,12) 1
(11,13) 3
(12,13) 2
(13,13) 1
(12,14) 3
(13,14) 2
(14,14) 1
(13,15) 3
(14,15) 2
(15,15) 1
(14,16) 3
(15,16) 2
(16,16) 1
(15,17) 3
(16,17) 2
(17,17) 1
(16,18) 3
(17,18) 2
(18,18) 1
(17,19) 3
(18,19) 2
(19,19) 1
(18,20) 3
(19,20) 2
(20,20) 1
(19,21) 3
(20,21) 2
(21,21) 1
(20,22) 3
(21,22) 2
(22,22) 1
(21,23) 3
(22,23) 2
(23,23) 1
(22,24) 3
(23,24) 2
(24,24) 1
(23,25) 3
(24,25) 2
(25,25) 1
F = full(DD)
F =
Columns 1 through 14
1 2 3 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 0 0 0 0 0 0 0 0 0 0
0 0 1 2 3 0 0 0 0 0 0 0 0 0
0 0 0 1 2 3 0 0 0 0 0 0 0 0
0 0 0 0 1 2 3 0 0 0 0 0 0 0
0 0 0 0 0 1 2 3 0 0 0 0 0 0
0 0 0 0 0 0 1 2 3 0 0 0 0 0
0 0 0 0 0 0 0 1 2 3 0 0 0 0
0 0 0 0 0 0 0 0 1 2 3 0 0 0
0 0 0 0 0 0 0 0 0 1 2 3 0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se
F 25x25 5000 double
ans 5x5 200 double
x 25x1 200 double
y 25x1 200 double
z 25x1 200 double
help sprand
sprand Sparse uniformly distributed random matrix.
R = sprand(S) has the same sparsity structure as S, but uniformly
distributed random entries.
R = sprand(m,n,density) is a random, m-by-n, sparse matrix with
approximately density*m*n uniformly distributed nonzero entries.
sprand is designed to produce large matrices with small density
and will generate significantly fewer nonzeros than requested
if m*n is small or density is large.
R = sprand(m,n,density,rc) also has reciprocal condition number
approximately equal to rc. R is constructed from a sum of
matrices of rank one.
If rc is a vector of length lr <= min(m,n), then R has
rc as its first lr singular values, all others are zero.
In this case, R is generated by random plane rotations
applied to a diagonal matrix with the given singular values
It has a great deal of topological and algebraic structure.
See also sprandn, sprandsym.
Documentation for sprand
Other functions named sprand
R = sprand(10,10,0.1)
R =
(9,2) 0.0497
(2,4) 0.2399
(2,5) 0.1233
(3,6) 0.1839
(4,6) 0.2400
(10,7) 0.9027
(1,9) 0.0760
(5,9) 0.4173
full(R)
ans =
Columns 1 through 8
0 0 0 0 0 0 0 0
0 0 0 0.2399 0.1233 0 0 0
0 0 0 0 0 0.1839 0 0
0 0 0 0 0 0.2400 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.0497 0 0 0 0 0 0
0 0 0 0 0 0 0.9027 0
Columns 9 through 10
0.0760 0
0 0
0 0
0 0
0.4173 0
0 0
0 0
0 0
0 0
0 0
diary off