2
ans =
2
10.2
ans =
10.2000
0.001
ans =
1.0000e-03
1.23456
ans =
1.2346
ans-1.2346
ans =
-4.0000e-05
format long e
1.23456
ans =
1.234560000000000e+00
2
ans =
2
1.2346
ans =
1.234600000000000e+00
1.2e-5
ans =
1.200000000000000e-05
1.2e-5
ans =
1.200000000000000e-05
3+5i
ans =
3.000000000000000e+00 + 5.000000000000000e+00i
sqrt(-1)
ans =
0 + 1.000000000000000e+00i
0/0
ans =
NaN
i
ans =
0 + 1.000000000000000e+00i
pi
ans =
3.141592653589793e+00
eps
ans =
2.220446049250313e-16
sin(pi)
ans =
1.224646799147353e-16
(1/49)*49
ans =
9.999999999999999e-01
A=1
A =
1
A
A =
1
size(A)
ans =
1 1
1:4
ans =
1 2 3 4
[1,2,3,4]
ans =
1 2 3 4
1:2:5
ans =
1 3 5
1:2:6
ans =
1 3 5
1:0.5:3
ans =
Column 1
1.000000000000000e+00
Column 2
1.500000000000000e+00
Column 3
2.000000000000000e+00
Column 4
2.500000000000000e+00
Column 5
3.000000000000000e+00
format
1:0.5:3
ans =
Columns 1 through 4
1.0000 1.5000 2.0000 2.5000
Column 5
3.0000
linspace(1,3,7)
ans =
Columns 1 through 4
1.0000 1.3333 1.6667 2.0000
Columns 5 through 7
2.3333 2.6667 3.0000
linspace(1,3,6)
ans =
Columns 1 through 4
1.0000 1.4000 1.8000 2.2000
Columns 5 through 6
2.6000 3.0000
linspace(1,3,5)
ans =
Columns 1 through 4
1.0000 1.5000 2.0000 2.5000
Column 5
3.0000
1:0.5:3
ans =
Columns 1 through 4
1.0000 1.5000 2.0000 2.5000
Column 5
3.0000
A=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
A(3,2)
ans =
10
pi=1
pi =
1
sin(pi)
ans =
0.8415
sin(1)
ans =
0.8415
i
ans =
0 + 1.0000i
i=5
i =
5
i
i =
5
i=sqrt(-1)
i =
0 + 1.0000i
asin(1)
ans =
1.5708
ans*2
ans =
3.1416
A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
clear A
A
??? Undefined function or variable 'A'.
pi
pi =
1
clear pi
pi
ans =
3.1416
clear i
i
ans =
0 + 1.0000i
A=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
A(3,2)
ans =
10
A(3,[1,2,3,4])
ans =
9 10 11 12
A(3,1:4)
ans =
9 10 11 12
A(3,:)
ans =
9 10 11 12
A(3,1:2)
ans =
9 10
A(3,1:3)
ans =
9 10 11
A(3,[1,2,3])
ans =
9 10 11
A(3,[2,1,3,4])
ans =
10 9 11 12
B=A(:,[2,1,3,4])
B =
2 1 3 4
6 5 7 8
10 9 11 12
14 13 15 16
A(3,end)
ans =
12
size(A)
ans =
4 4
A(1:2,2:3)
ans =
2 3
6 7
x=0.1234e-2
x =
0.0012
format long e
x=0.1234e-2
x =
1.234000000000000e-03
((1+x)-1)/x
ans =
9.999999999999654e-01
x=8e-16
x =
8.000000000000000e-16
((1+x)-1)/x
ans =
1.110223024625157e+00
1
ans =
1
1.00000
ans =
1
1/2
ans =
5.000000000000000e-01
1/4
ans =
2.500000000000000e-01
1/8
ans =
1.250000000000000e-01
1/16
ans =
6.250000000000000e-02
A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
diag(A)
ans =
1
6
11
16
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.
Overloaded functions or methods (ones with the same name in other directories)
help frd/diag.m
help sym/diag.m
Reference page in Help browser
doc diag
doc diag
Overloaded functions or methods (ones with the same name in other directories)
doc fixedpoint/diag
doc symbolic/diag
doc diag
Overloaded functions or methods (ones with the same name in other directories)
doc fixedpoint/diag
doc symbolic/diag
A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
diag(A,-1)
ans =
5
10
15
diag(A,0)
ans =
1
6
11
16
reshape(A,2,8)
ans =
Columns 1 through 7
1 9 2 10 3 11 4
5 13 6 14 7 15 8
Column 8
12
16
A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
reshape(A,2,8)
ans =
Columns 1 through 7
1 9 2 10 3 11 4
5 13 6 14 7 15 8
Column 8
12
16
reshape(A,8,2)
ans =
1 3
5 7
9 11
13 15
2 4
6 8
10 12
14 16
reshape(A,8,3)
??? Error using ==> reshape
To RESHAPE the number of elements must not change.
reshape(A,1,16)
ans =
Columns 1 through 7
1 5 9 13 2 6 10
Columns 8 through 14
14 3 7 11 15 4 8
Columns 15 through 16
12 16
reshape(A,16,1)
ans =
1
5
9
13
2
6
10
14
3
7
11
15
4
8
12
16
A(:)
ans =
1
5
9
13
2
6
10
14
3
7
11
15
4
8
12
16
repmat([1,2;3,4],3,2)
ans =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
diag(A)
ans =
1
6
11
16
diag([1,2,3,4])
ans =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
diag([1,2,3],1)
ans =
0 1 0 0
0 0 2 0
0 0 0 3
0 0 0 0
diag([1,2,3],-2)
ans =
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
zeros(2,3)
ans =
0 0 0
0 0 0
ones(3,2)
ans =
1 1
1 1
1 1
eye(3)
ans =
1 0 0
0 1 0
0 0 1
rand
ans =
8.147236863931789e-01
rand(2,3)
ans =
Column 1
9.057919370756192e-01
1.269868162935061e-01
Column 2
9.133758561390194e-01
6.323592462254095e-01
Column 3
9.754040499940952e-02
2.784982188670484e-01
format
rand(2,3)
ans =
0.5469 0.9649 0.9706
0.9575 0.1576 0.9572
randn(2,3)
ans =
-0.4326 0.1253 -1.1465
-1.6656 0.2877 1.1909
A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
A.'1 2 3 4
??? A.'1 2 3 4
|
Error: Unexpected MATLAB expression.
A.'
ans =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
A'
ans =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
[1+i,2-i;3,4+i]
ans =
1.0000 + 1.0000i 2.0000 - 1.0000i
3.0000 4.0000 + 1.0000i
[1+i,2-i;3,4+i]'
ans =
1.0000 - 1.0000i 3.0000
2.0000 + 1.0000i 4.0000 - 1.0000i
[1+i,2-i;3,4+i]
ans =
1.0000 + 1.0000i 2.0000 - 1.0000i
3.0000 4.0000 + 1.0000i
[1+i,2-i;3,4+i].'
ans =
1.0000 + 1.0000i 3.0000
2.0000 - 1.0000i 4.0000 + 1.0000i
[1,2,3]
ans =
1 2 3
[1;2;3]
ans =
1
2
3
[1,2,3]'
ans =
1
2
3
[1,2,3].'
ans =
1
2
3
A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
triu(A)
ans =
1 2 3 4
0 6 7 8
0 0 11 12
0 0 0 16
tril(A)
ans =
1 0 0 0
5 6 0 0
9 10 11 0
13 14 15 16
A=[1,0,0;2,2,0;0,0,3]
A =
1 0 0
2 2 0
0 0 3
sparse(A)
ans =
(1,1) 1
(2,1) 2
(2,2) 2
(3,3) 3
B=sparse(A)
B =
(1,1) 1
(2,1) 2
(2,2) 2
(3,3) 3
B(2,1)
ans =
2
a=[1;2;3;4]
a =
1
2
3
4
b=[0;10;20;30]
b =
0
10
20
30
c=[100;200;0;0]
c =
100
200
0
0
A=spdiags([c,a,b],[-2,0,1],4,4)
A =
(1,1) 1
(3,1) 100
(1,2) 10
(2,2) 2
(4,2) 200
(2,3) 20
(3,3) 3
(3,4) 30
(4,4) 4
full(A)
ans =
1 10 0 0
0 2 20 0
100 0 3 30
0 200 0 4
diary off