Arrays 一次跨多个维度平均多维数组

Arrays 一次跨多个维度平均多维数组,arrays,matlab,Arrays,Matlab,假设我有一个5维数组X,我想一次计算其元素在多个维度上的平均值(标量),例如,前两个维度的最后三个维度给定了指定值 我尝试了mean(X(1,1,:,:,:,:),但这并没有给我想要的结果,即它产生数组输出而不是标量输出 我的解决方法是循环计算每个维度的平均值,然后手动计算所有这些部分(边际)平均值的平均值。但这很麻烦,因为它需要编写更多的代码,这常常会让我感到困惑 有没有一个简单的技巧可以通过调用与上面类似的mean函数来实现这一目标?您需要首先重塑数据,以便将最后三个维度展平,然后沿第三个维

假设我有一个5维数组X,我想一次计算其元素在多个维度上的平均值(标量),例如,前两个维度的最后三个维度给定了指定值

我尝试了
mean(X(1,1,:,:,:,:)
,但这并没有给我想要的结果,即它产生数组输出而不是标量输出

我的解决方法是循环计算每个维度的平均值,然后手动计算所有这些部分(边际)平均值的平均值。但这很麻烦,因为它需要编写更多的代码,这常常会让我感到困惑


有没有一个简单的技巧可以通过调用与上面类似的
mean
函数来实现这一目标?

您需要首先
重塑数据,以便将最后三个维度展平,然后沿第三个维度取其平均值

M = mean(reshape(X, size(X, 1), size(X, 2), []), 3);

这里的好处是,这不会在内存中创建
X
的副本,因为
重塑
只会更改访问相同数据的方式。

您需要首先
重塑
数据,以便将最后三个维度展平,然后沿第三个维度取其平均值

M = mean(reshape(X, size(X, 1), size(X, 2), []), 3);

这里的好处是,这不会在内存中创建
X
的副本,因为
重塑
只会更改访问相同数据的方式。

这里有一种方法可以沿前两个维度对特定元素执行平均/平均计算-

% Random input array and specific indices for dimension-1,2
X = randi(9,3,5,4,2,4);
dim12_idx = [1,1; 2,4; 3,1; 3,3]

% Store size parameters
[d1,d2,~] = size(X);

% Reshape input array to 2D merging first two dims as one, merging rest as other 
Xr = reshape(X,d1*d2,[]);

% Calculate the linear index equivalent of the specific indices
lidx = sub2ind([d1,d2],dim12_idx(:,1),dim12_idx(:,2));

% Index into the rows of reshaped array with those and perform mean along
% columns for the final output
out = mean(Xr(lidx,:),2)
原始方法的问题

现在,您的
mean(X(1,1,:,:,:,:)
方法将不起作用,因为它将只沿一个维度执行
mean
计算,我刚刚发现这将是第一个非单体维度(意外发现的非常有用的信息)。要使其作为一个整体沿最后三个维度工作,可以将其重塑为列向量,然后沿第一个维度使用
mean
,如下所示-

mean(reshape(X(1,1,:,:,:),[],1))
让我们在下一节中使用它来验证结果

使用列出的输入运行样本进行验证

>> dim12_idx
dim12_idx =
     1     1
     2     4
     3     1
     3     3
>> mean(reshape(X(1,1,:,:,:),[],1))
ans =
       5.3125
>> mean(reshape(X(2,4,:,:,:),[],1))
ans =
       5.0312
>> mean(reshape(X(3,1,:,:,:),[],1))
ans =
       4.5312
>> mean(reshape(X(3,3,:,:,:),[],1))
ans =
        4.875
>> out
out =
       5.3125
       5.0312
       4.5312
        4.875

这是一种沿前两个维度对特定元素执行平均值/平均值计算的方法-

% Random input array and specific indices for dimension-1,2
X = randi(9,3,5,4,2,4);
dim12_idx = [1,1; 2,4; 3,1; 3,3]

% Store size parameters
[d1,d2,~] = size(X);

% Reshape input array to 2D merging first two dims as one, merging rest as other 
Xr = reshape(X,d1*d2,[]);

% Calculate the linear index equivalent of the specific indices
lidx = sub2ind([d1,d2],dim12_idx(:,1),dim12_idx(:,2));

% Index into the rows of reshaped array with those and perform mean along
% columns for the final output
out = mean(Xr(lidx,:),2)
原始方法的问题

现在,您的
mean(X(1,1,:,:,:,:)
方法将不起作用,因为它将只沿一个维度执行
mean
计算,我刚刚发现这将是第一个非单体维度(意外发现的非常有用的信息)。要使其作为一个整体沿最后三个维度工作,可以将其重塑为列向量,然后沿第一个维度使用
mean
,如下所示-

mean(reshape(X(1,1,:,:,:),[],1))
让我们在下一节中使用它来验证结果

使用列出的输入运行样本进行验证

>> dim12_idx
dim12_idx =
     1     1
     2     4
     3     1
     3     3
>> mean(reshape(X(1,1,:,:,:),[],1))
ans =
       5.3125
>> mean(reshape(X(2,4,:,:,:),[],1))
ans =
       5.0312
>> mean(reshape(X(3,1,:,:,:),[],1))
ans =
       4.5312
>> mean(reshape(X(3,3,:,:,:),[],1))
ans =
        4.875
>> out
out =
       5.3125
       5.0312
       4.5312
        4.875

您想要前两列的每个唯一值的所有垂直值和水平值的平均值?您想要前两列的每个唯一值的所有垂直值和水平值的平均值?您不能只做
[d1,d2,d3]=大小(X)
d3
将自动成为
d3*d4*d5
?难道你不能只做
[d1,d2,d3]=大小(X)
d3
将自动成为
d3*d4*d5