空数组;一个数的数组为1维的;1行4列; 4行1列;1到20差1组成所有元素;1到20间隔2组成所有的元素;1到20平均分配为11个元素
>> A=[]
A =
[]
>> A
A =
[]
>> A=[2]
A =
2
>> A=2
A =
2
>> A=[1,2,3,4]
A =
1 2 3 4
>> A=[1;2;3;4]
A =
1
2
3
4
>> A=[1,2,3,4]
A =
1 2 3 4
>> A=[1,2,3,4]'
A =
1
2
3
4
>> A=[1:1:20]
A =
Columns 1 through 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Columns 17 through 20
17 18 19 20
>> A=[1:2:20]
A =
1 3 5 7 9 11 13 15 17 19
>> A=[1:50]
A =
Columns 1 through 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Columns 17 through 32
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Columns 33 through 48
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Columns 49 through 50
49 50
>> A=linspace(1,20,2)
A =
1 20
>> A=linspace(1,20,20/2)
A =
1.0000 3.1111 5.2222 7.3333 9.4444 11.5556 13.6667 15.7778 17.8889 20.0000
>> A=linspace(1,20,11)
A =
Columns 1 through 10
1.0000 2.9000 4.8000 6.7000 8.6000 10.5000 12.4000 14.3000 16.2000 18.1000
Column 11
20.0000
数组A是几行几列的; 几行几列中最大值为数组的长度;数组的维数;
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> size(A)
ans =
3 3
>> [a,b]=size(A)
a =
3
b =
3
>> length(A)
ans =
3
>> A
A =
1 2 3
4 5 6
7 8 9
>> ndims(A)
ans =
2
>> length(size(A))
ans =
2
内存中的变量和占用大小
>> who
Your variables are:
A a ans b x y z
>> whos
Name Size Bytes Class Attributes
A 3x3 72 double
a 1x1 8 double
ans 1x1 8 double
b 1x1 8 double
x 1x8 64 double
y 5x1 40 double
z 1x5 40 double
全0数组;全1数组;E数组;对角线数组;
入参A为一行或者一列数组,则diag(A)为1行在对角线上组成数组; diag(A,2)表示往右移动2列的对角线上,diag(A,-2)表示往下移动2行的对角线上。
入参为大于1行大于1列的数组,则diag(A)为取出对角线数组成1行的数组;diag(A,2)为取出往右移动2列的对角线组成1行数组
>> zero(3,3)
Undefined function 'zero' for input arguments of type 'double'.
>> zeros(3,3)
ans =
0 0 0
0 0 0
0 0 0
>> zeros(size(A))
ans =
0 0 0
0 0 0
0 0 0
>> ones(3,3)
ans =
1 1 1
1 1 1
1 1 1
>> who
Your variables are:
A a ans b x y z
>> eye(2)
ans =
1 0
0 1
>> eye(2,2)
ans =
1 0
0 1
>> eye(2,3)
ans =
1 0 0
0 1 0
>> eye(3,2)
ans =
1 0
0 1
0 0
>> A=[1,2,3,4,5,6,7]
A =
1 2 3 4 5 6 7
>> diag(A)
ans =
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 5 0 0
0 0 0 0 0 6 0
0 0 0 0 0 0 7
>> diag(A,2)
ans =
0 0 1 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0
0 0 0 0 3 0 0 0 0
0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 5 0 0
0 0 0 0 0 0 0 6 0
0 0 0 0 0 0 0 0 7
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
>> diag(A,-2)
ans =
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0
0 0 0 0 5 0 0 0 0
0 0 0 0 0 6 0 0 0
0 0 0 0 0 0 7 0 0
>> A=[1,2,3,4;1 2 3 4;1 2 3 4;1 2 3 4]
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
>> 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
>> diag(A)
ans =
1
6
11
16
>> diag(A,2)
ans =
3
8
>> diag(ans,2)
ans =
0 0 3 0
0 0 0 8
0 0 0 0
0 0 0 0
>> diag(ans,3)
ans =
0
>> ans=[3 8]'
ans =
3
8
>> diag(ans,3)
ans =
0 0 0 3 0
0 0 0 0 8
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> ans=[3 8]
ans =
3 8
>> diag(ans,2)
ans =
0 0 3 0
0 0 0 8
0 0 0 0
0 0 0 0
生成随机数组;生成magic数组即行列和值一样;一个下标和混合下表选择数组中某一元素;生成对应的逻辑数组;原数组和逻辑数组组合选择
A=rand(2)
A =
0.8147 0.1270
0.9058 0.9134
>> A=rand(2,3)
A =
0.6324 0.2785 0.9575
0.0975 0.5469 0.9649
>> magic(3)
ans =
8 1 6
3 5 7
4 9 2
>> A(1)
ans =
0.6324
>> A(1,2)
ans =
0.2785
>> A(2)
ans =
0.0975
>> A
A =
0.6324 0.2785 0.9575
0.0975 0.5469 0.9649
>> B=A>0.7
B =
0 0 1
0 0 1
>> A(B)=0
A =
0.6324 0.2785 0
0.0975 0.5469 0
数组上下组合;数组左右组合;只会说上下左右,即1,2因此1代表上下;
数组作为整体构造m*n的数组;
构造新数组
A 0
0 B
构造新数组B作为整体扩展为A的大小新数组,并分别乘以相应的A的数值
>> cat(1,A,B)
ans =
0.6324 0.2785 0
0.0975 0.5469 0
0 0 1.0000
0 0 1.0000
>> cat(2,A,B)
ans =
0.6324 0.2785 0 0 0 1.0000
0.0975 0.5469 0 0 0 1.0000
>> horzcat(A,B)
ans =
0.6324 0.2785 0 0 0 1.0000
0.0975 0.5469 0 0 0 1.0000
>> vertcat(A,B)
ans =
0.6324 0.2785 0
0.0975 0.5469 0
0 0 1.0000
0 0 1.0000
>> A
A =
0.6324 0.2785 0
0.0975 0.5469 0
>> repmat(A,2,2)
ans =
0.6324 0.2785 0 0.6324 0.2785 0
0.0975 0.5469 0 0.0975 0.5469 0
0.6324 0.2785 0 0.6324 0.2785 0
0.0975 0.5469 0 0.0975 0.5469 0
>> A
A =
0.6324 0.2785 0
0.0975 0.5469 0
>> B
B =
0 0 1
0 0 1
>> blkdiag(A,B)
ans =
0.6324 0.2785 0 0 0 0
0.0975 0.5469 0 0 0 0
0 0 0 0 0 1.0000
0 0 0 0 0 1.0000
>> kron(A,B)
ans =
0 0 0.6324 0 0 0.2785 0 0 0
0 0 0.6324 0 0 0.2785 0 0 0
0 0 0.0975 0 0 0.5469 0 0 0
0 0 0.0975 0 0 0.5469 0 0 0
数组的扩充和裁剪;
>> A
A =
0.6324 0.2785 0
0.0975 0.5469 0
>> A(4,4)=4
A =
0.6324 0.2785 0 0
0.0975 0.5469 0 0
0 0 0 0
0 0 0 4.0000
>> A([1,3],[2,3])
ans =
0.2785 0
0 0
>> A([1:4],[2,3])
ans =
0.2785 0
0.5469 0
0 0
0 0
>> A([1,3],[2,3])=[]
Subscripted assignment dimension mismatch.
>> A([1:4],[2,3])=[]
Subscripted assignment dimension mismatch.
>> A([2,3],[2,3])=[]
Subscripted assignment dimension mismatch.
>> A
A =
0.6324 0.2785 0 0
0.0975 0.5469 0 0
0 0 0 0
0 0 0 4.0000
>> A(1,1)=[]
Subscripted assignment dimension mismatch.
>> A([1,2,3,4])=[]
A =
Columns 1 through 10
0.2785 0.5469 0 0 0 0 0 0 0 0
Columns 11 through 12
0 4.0000
>> A
A =
Columns 1 through 10
0.2785 0.5469 0 0 0 0 0 0 0 0
Columns 11 through 12
0 4.0000
>> A=magic(7)
A =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
>> A([1,2],[2])=[]
Subscripted assignment dimension mismatch.
>> A([1:7],[2])=[]
Subscripted assignment dimension mismatch.
数组的运算翻转
>> A
A =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
>> A'
ans =
30 38 46 5 13 21 22
39 47 6 14 15 23 31
48 7 8 16 24 32 40
1 9 17 25 33 41 49
10 18 26 34 42 43 2
19 27 35 36 44 3 11
28 29 37 45 4 12 20
>> ctranspose(A)
ans =
30 38 46 5 13 21 22
39 47 6 14 15 23 31
48 7 8 16 24 32 40
1 9 17 25 33 41 49
10 18 26 34 42 43 2
19 27 35 36 44 3 11
28 29 37 45 4 12 20
>> transpose(A)
ans =
30 38 46 5 13 21 22
39 47 6 14 15 23 31
48 7 8 16 24 32 40
1 9 17 25 33 41 49
10 18 26 34 42 43 2
19 27 35 36 44 3 11
28 29 37 45 4 12 20
>> A'
ans =
30 38 46 5 13 21 22
39 47 6 14 15 23 31
48 7 8 16 24 32 40
1 9 17 25 33 41 49
10 18 26 34 42 43 2
19 27 35 36 44 3 11
28 29 37 45 4 12 20
>> A'
ans =
30 38 46 5 13 21 22
39 47 6 14 15 23 31
48 7 8 16 24 32 40
1 9 17 25 33 41 49
10 18 26 34 42 43 2
19 27 35 36 44 3 11
28 29 37 45 4 12 20
>> A
A =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
数组的加减乘除
>> A=magic(3,3)
Error using magic
Too many input arguments.
>> A=magic(3)
A =
8 1 6
3 5 7
4 9 2
>> B=rand(3)
B =
0.1576 0.4854 0.4218
0.9706 0.8003 0.9157
0.9572 0.1419 0.7922
>> A/B
ans =
-2.7779 1.6720 7.1199
17.6113 -5.4560 5.7668
-12.9623 21.9313 -15.9255
>> A*inv(B)
ans =
-2.7779 1.6720 7.1199
17.6113 -5.4560 5.7668
-12.9623 21.9313 -15.9255
>> A\B
ans =
-0.0558 -0.0351 -0.0196
0.1130 0.0031 0.0782
0.0819 0.1271 0.0834
>> inv(A)*B
ans =
-0.0558 -0.0351 -0.0196
0.1130 0.0031 0.0782
0.0819 0.1271 0.0834
>> A
A =
8 1 6
3 5 7
4 9 2
>> C=A>5
C =
1 0 1
0 0 1
0 1 0
>> find(C)
ans =
1
6
7
8
>> A(find(C))
ans =
8
9
6
7
>> find(A)
ans =
1
2
3
4
5
6
7
8
9
>> A
A =
8 1 6
3 5 7
4 9 2
>> sort(A)
ans =
3 1 2
4 5 6
8 9 7
>>