考虑MATLAB中的以下矩阵:
% x y z V
data = [ 1 1 1 6
1 1 2 7
1 1 3 8
1 2 1 6
1 2 2 7
1 2 3 9
1 3 1 6
1 3 2 4
1 3 3 8
2 1 1 9
2 1 2 8]
如何通过对适当的V
和z
进行平均来获得(x
vs y
)个人资料?
在这种情况下,结果应为:
B = [1, (6+6+6+9)/4);
2, (7+7+4+8)/4);
3, (8+9+9)/3 ]
平均值超过了具有相同V
的所有Z
。
但是,原始数据包含实际值:
0.815210000000000 1.30799000000000 -84.8230000000000 -5.90987000000000
0.815210000000000 2.42194000000000 -84.8230000000000 -0.236048000000000
0.815210000000000 3.53589000000000 -84.8230000000000 3.19780000000000
0.815210000000000 4.64984000000000 -84.8230000000000 5.48969000000000
0.815210000000000 5.76379000000000 -84.8230000000000 6.42475000000000
0.815210000000000 6.87774000000000 -84.8230000000000 5.97255000000000
0.815210000000000 7.99169000000000 -84.8230000000000 4.25794000000000
0.815210000000000 9.10564000000000 -84.8230000000000 1.49070000000000
0.815210000000000 10.2196000000000 -84.8230000000000 -2.10373000000000
答案 0 :(得分:1)
使用accumarray
查找组的平均值。在第3列的unique
值列前添加B
。
[C, ~, ic]= unique(data(:,3));
B = [C accumarray(ic,data(:,4),[],@mean)]
小心浮点值,如果第三列的值不是真的完全相同,那么请改用uniquetol
。
答案 1 :(得分:0)
我使用其他语言的编程知识解决了这个问题:
A = sortrows(data, 3);
G = unique(A(:, 3));
n = size(G,1);
num = size(A,1);
B = zeros(n,2);
i =1;
oldZ=A(1,3);
sum = 0;
m =0;
for row=1:num
z = A(row,3);
if z ~= oldZ
B(i,1) =oldZ;
B(i,2)= sum/m;
sum = 0;
m =0;
oldZ = z;
i = i+1;
end
V = A(row,4);
sum = sum + V;
m = m +1;
end
B(i,1) =oldZ;
B(i,2)= sum/m;
B是请求的配置文件,它是沿z的xy平面的平均值。