我有一个22007x3矩阵,第3列有数据,第1和第2列有两个独立的索引.
例如.
x =
1 3 4
1 3 5
1 3 5
1 16 4
1 16 3
1 16 4
2 4 1
2 4 3
2 11 2
2 11 3
2 11 2
Run Code Online (Sandbox Code Playgroud)
当第1列中的值相同且第2列中的值相同时,我需要找到第3列中值的平均值,最终得到如下结果:
ans =
1 3 4.6667
1 16 3.6667
2 4 2
2 11 2.3333
Run Code Online (Sandbox Code Playgroud)
请记住,在我的数据中,第1列和第2列中的值发生的次数可能不同.
我已经尝试过的两个选项是meshgrid/ accumarray选项,使用两个不同的unique函数和一个3D数组:
[U, ix, iu] = unique(x(:, 1));
[U2,ix2,iu2] = unique(x(:,2));
[c, r, j] = meshgrid((1:size(x(:, 1), 2)), iu, iu2);
totals = accumarray([r(:), c(:), j(:)], x(:), [], @nanmean);
Run Code Online (Sandbox Code Playgroud)
这给了我这个:
??? Maximum variable size allowed by the program is exceeded.
Error in ==> meshgrid at 60
xx = xx(ones(ny,1),:,ones(nz,1));
Run Code Online (Sandbox Code Playgroud)
和循环选项,
for i=1:size(x,1)
if x(i,2)== x(i+1,2);
totals(i,:)=accumarray(x(:,1),x(:,3),[],@nanmean);
end
end
Run Code Online (Sandbox Code Playgroud)
这显然是非常非常错误的,尤其是因为这x(i+1,2)一点.
我也在考虑根据第1列中的值出现多少次来创建单独的矩阵,但这样会很长且效率低下,所以我不愿意走这条路.
使用a对前两列进行分组unique(...,'rows'),然后仅累积第三列(始终是仅在累积真正发生的地方积累的最佳方法,从而避免索引,即前两列,您可以重新连接unX):
[unX,~,subs] = unique(x(:,1:2),'rows');
out = [unX accumarray(subs,x(:,3),[],@nanmean)];
out =
1 3 4.6667
1 16 3.6667
2 4 2
2 11 2.33
Run Code Online (Sandbox Code Playgroud)