[英]How to perform operations along a certain dimension of an array?
I have a 3D array containing five 3-by-4 slices, defined as follows: 我有一个包含5个3×4切片的3D数组,定义如下:
rng(3372061);
M = randi(100,3,4,5);
I'd like to collect some statistics about the array: 我想收集有关数组的一些统计信息:
This is quite straightforward using loops, 这很简单,使用循环,
sz = size(M);
colMax = zeros(1,4,5);
rowMean = zeros(3,1,5);
sliceSTD = zeros(1,1,5);
for indS = 1:sz(3)
sl = M(:,:,indS);
sliceSTD(indS) = std(sl(1:sz(1)*sz(2)));
for indC = 1:sz(1)
rowMean(indC,1,indS) = mean(sl(indC,:));
end
for indR = 1:sz(2)
colMax(1,indR,indS) = max(sl(:,indR));
end
end
But I'm not sure that this is the best way to approach the problem. 但我不确定这是解决问题的最佳方法。
A common pattern I noticed in the documentation of max
, mean
and std
is that they allow to specify an additional dim
input. 我在
max
, mean
和std
文档中注意到的一个常见模式是它们允许指定一个额外的dim
输入。 For instance, in max
: 例如,在
max
:
M = max(A,[],dim)
returns the largest elements along dimensiondim
.M = max(A,[],dim)
返回维度dim
的最大元素。 For example, ifA
is a matrix, thenmax(A,[],2)
is a column vector containing the maximum value of each row.例如,如果
A
是矩阵,则max(A,[],2)
是包含每行最大值的列向量。
How can I use this syntax to simplify my code? 如何使用此语法来简化我的代码?
Many functions in MATLAB allow the specification of a "dimension to operate over" when it matters for the result of the computation (several common examples are: min
, max
, sum
, prod
, mean
, std
, size
, median
, prctile
, bounds
) - which is especially important for multidimensional inputs. MATLAB中的许多函数允许在计算结果很重要时指定“维度操作”(几个常见的例子是:
min
, max
, sum
, prod
, mean
, std
, size
, median
, prctile
, bounds
) - 这对于多维输入尤为重要。 When the dim
input is not specified, MATLAB has a way of choosing the dimension on its own, as explained in the documentation; 当
dim
没有指定输入,MATLAB有选择自身的尺寸,如文档中阐述的一种方式; for example in max
: 例如在
max
:
- If
A
is a vector, thenmax(A)
returns the maximum ofA
.如果
A
是一个向量,那么max(A)
返回最大的A
。- If
A
is a matrix, thenmax(A)
is a row vector containing the maximum value of each column.如果
A
是矩阵,则max(A)
是包含每列最大值的行向量。- If
A
is a multidimensional array, thenmax(A)
operates along the first array dimension whose size does not equal1
, treating the elements as vectors.如果
A
是多维数组,则max(A)
沿着第一个不等于1
数组维操作,将元素视为向量。 The size of this dimension becomes1
while the sizes of all other dimensions remain the same.此尺寸的大小变为
1
而所有其他尺寸的尺寸保持不变。 IfA
is an empty array whose first dimension has zero length, thenmax(A)
returns an empty array with the same size asA
.如果
A
是第一个维度为零长度的空数组,则max(A)
返回一个与A
大小相同的空数组。
Then, using the ...,dim)
syntax we can rewrite the code as follows: 然后,使用
...,dim)
语法,我们可以重写代码如下:
rng(3372061);
M = randi(100,3,4,5);
colMax = max(M,[],1);
rowMean = mean(M,2);
sliceSTD = std(reshape(M,1,[],5),0,2); % we use `reshape` to turn each slice into a vector
This has several advantages: 这有几个好处:
In conclusion: it is always a good idea to read the documentation of functions you're using, and experiment with different syntaxes, so as not to miss similar opportunities to make your code more succinct. 总之:阅读您正在使用的函数的文档并尝试不同的语法总是一个好主意,以免错过使您的代码更简洁的类似机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.