博主github:https://github.com/MichaelBeechan
博主CSDN:https://blog.csdn.net/u011344545
代码下载:https://github.com/MichaelBeechan/Matlab-From-Zero-To-One
%% Time:2019.5.26
%% Function:数组类型
%% 多维数组
R = randn(3,4,5);
% 生成 4! = 24 置换1:4。第 k 个置换为行向量 p(k,:)
p = perms(1:4); % 排列组合
A = magic(4);
M = zeros(4, 4, 24);
for k = 1 : 24
M(:, :, k) = A(:, p(k, :));
end
size(M);
% 通过改变第 d 个下标来计算总和
%sum(M, d)
S1 = sum(M, 1); %1×4×24
S2 = sum(M, 2); %4×1×24
S3 = sum(M, 3); %4×4×1
M = ones(2, 2, 4);
S4 = sum(M, 3)
sum(M, 3)解释:
对于M是一个多维矩阵的情况
此处3可理解为RGB图像的R、G、B分量
求Sum则只是将对应的分量相加即可,即R+G+B
如:
M = ones(2, 2, 4)
S4 = sum(M, 3)
M(:,:,1) =
1 1
1 1
M(:,:,2) =
1 1
1 1
M(:,:,3) =
1 1
1 1
M(:,:,4) =
1 1
1 1
S4 =
4 4
4 4