Matlab刻意训练
Task06:多维数组(3.18)
多维数组的结构
多维数组的一般表达式为:
3.18.1 多维数组的创建
(1) 多维数组的创建可以通过直接赋值来取得。
>> A(:,:,1)=magic(3)
A =
8 1 6
3 5 7
4 9 2
>> A(:,:,2)=vander([1 2 3])
A(:,:,1) =
8 1 6
3 5 7
4 9 2
A(:,:,2) =
1 1 1
4 2 1
9 3 1
>> A(:,:,3)=pascal(3)
A(:,:,1) =
8 1 6
3 5 7
4 9 2
A(:,:,2) =
1 1 1
4 2 1
9 3 1
A(:,:,3) =
1 1 1
1 2 3
1 3 6
(2) 通过矩阵函数生成多维函数
- 生成三维正态分布的随机矩阵B, m=n=4,k=3。
>> B=rand(4,4,3)
B(:,:,1) = %随机矩阵第一页
0.4218 0.6557 0.6787 0.6555
0.9157 0.0357 0.7577 0.1712
0.7922 0.8491 0.7431 0.7060
0.9595 0.9340 0.3922 0.0318
B(:,:,2) = %随机矩阵第二页
0.2769 0.6948 0.4387 0.1869
0.0462 0.3171 0.3816 0.4898
0.0971 0.9502 0.7655 0.4456
0.8235 0.0344 0.7952 0.6463
B(:,:,3) = %随机矩阵第三页
0.7094 0.6551 0.9597 0.7513
0.7547 0.1626 0.3404 0.2551
0.2760 0.1190 0.5853 0.5060
0.6797 0.4984 0.2238 0.6991
- 用矩阵复制生成多维矩阵
>> A=[1 2;3 4] %样板矩阵
A =
1 2
3 4
>> C= repmat(A,[3,4,3]) %将矩阵A复制成3*4, 复制后再复制3页
C(:,:,1) =
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
C(:,:,2) =
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
C(:,:,3) =
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
- 矩阵连接函数cat
>> A=magic(3)
A =
8 1 6
3 5 7
4 9 2
>> B=ones(3)
B =
1 1 1
1 1 1
1 1 1
>> C=eye(3)
C =
1 0 0
0 1 0
0 0 1
>> D=cat(3,A,B,C)
D(:,:,1) =
8 1 6
3 5 7
4 9 2
D(:,:,2) =
1 1 1
1 1 1
1 1 1
D(:,:,3) =
1 0 0
0 1 0
0 0 1
3.18.2 多维数组的运算
>> B(:,:,1)=magic(3);
>> B(:,:,2)=ones(3);
>> B(:,:,3)=eye(3);
>> size(B) %B矩阵的大小为3行,3列,3页
ans =
3 3 3
>> sum(B) %数组求和
ans(:,:,1) =
15 15 15
ans(:,:,2) =
3 3 3
ans(:,:,3) =
1 1 1
>> prod(B) %数组求乘积
ans(:,:,1) =
96 45 84
ans(:,:,2) =
1 1 1
ans(:,:,3) =
0 0 0
来源:https://blog.csdn.net/Connor_Pang/article/details/102749675