简体   繁体   English

如何沿阵列的某个维度执行操作?

[英]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: 我想收集有关数组的一些统计信息:

  • The maximum value in every column. 每列中的最大值。
  • The mean value in every row. 每行的平均值。
  • The standard deviation within each slice. 每个切片内的标准偏差。

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. 我在maxmeanstd文档中注意到的一个常见模式是它们允许指定一个额外的dim输入。 For instance, in max : 例如,在max

M = max(A,[],dim) returns the largest elements along dimension dim . M = max(A,[],dim)返回维度dim的最大元素。 For example, if A is a matrix, then max(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中的许多函数允许在计算结果很重要时指定“维度操作”(几个常见的例子是: minmaxsumprodmeanstdsizemedianprctilebounds ) - 这对于多维输入尤为重要。 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, then max(A) returns the maximum of A . 如果A是一个向量,那么max(A)返回最大的A
  • If A is a matrix, then max(A) is a row vector containing the maximum value of each column. 如果A是矩阵,则max(A)是包含每列最大值的行向量。
  • If A is a multidimensional array, then max(A) operates along the first array dimension whose size does not equal 1 , treating the elements as vectors. 如果A是多维数组,则max(A)沿着第一个不等于1数组维操作,将元素视为向量。 The size of this dimension becomes 1 while the sizes of all other dimensions remain the same. 此尺寸的大小变为1而所有其他尺寸的尺寸保持不变。 If A is an empty array whose first dimension has zero length, then max(A) returns an empty array with the same size as A . 如果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: 这有几个好处:

  • The code is easier to understand. 代码更容易理解。
  • The code is potentially more robust, being able to handle inputs beyond those it was initially designed for. 代码可能更强大,能够处理超出最初设计的输入。
  • The code is likely faster. 代码可能更快。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM