我在MATLAB中有大量的数据(350695x5)。一个例子是:
z = [
1.79 0.16 0.16 21.39 21.50
1.83 0.16 0.16 21.39 22.40
1.92 0.16 0.16 21.39 22.00
2.07 0.16 0.16 21.39 22.00
2.36 0.15 0.15 21.39 21.08
2.96 0.13 0.13 21.39 21.04
3.21 0.13 0.13 21.39 23.00
3.72 0.12 0.12 21.39 24.00
3.87 0.11 0.11 21.39 21.39
4.14 0.10 0.10 21.39 22.00
4.14 0.10 0.10 21.39 21.50
4.16 0.10 0.10 21.39 21.39] 我需要按以下方式对其进行排序:基于1-2,2-3,3-4的1列,并为2,3,4列找到范围(0-1,1-2,2-3,3-4)的平均值。
结果应该如下所示:
1 0.16 0.16 21.39 21.97
2 0.15 0.15 21.39 21.49
3 0.12 0.12 21.39 22.68
4 0.10 0.10 21.39 21.63问题是我不能用适当的方法来分类。
解决方案的部分可由
[ii jj] = ndgrid(z(:,1)+1,1:size(z,2)-1) %should sort first column from 0-1,1-2, 2-3, 3-4
z23 = z(:,2:end)
out = [unique(z(:,1)),accumarray([ii(:),jj(:)],z23(:),[],@mean)], %find mean value发布于 2016-11-09 11:47:35
试试这个:
idx = floor(z(:, 1));
sub = [idx z(:, 2:5)];
[xx, yy] = ndgrid(idx, 1:size(sub, 2));
out = accumarray([xx(:) yy(:)], sub(:), [], @mean)
out =
1.0000 0.1600 0.1600 21.3900 21.9667
2.0000 0.1467 0.1467 21.3900 21.3733
3.0000 0.1200 0.1200 21.3900 22.7967
4.0000 0.1000 0.1000 21.3900 21.6300结果和你的不完全吻合。我不确定我是否确切地理解了您想要的内容,但是我编写的代码计算了范围1 <= x < 2、2 <= x < 3等的平均值。
发布于 2016-11-09 09:38:23
使用逻辑索引查找适用于特定范围的z中的值,例如:
i01 = (z >= 0) & (z < 1); % Find logical indices
z01 = z(i01); % Get values from 0 up to 1 (but not including 1)然后,计算平均值很容易:mu_z01 = mean(z01);。当然,同样的方法也适用于其他范围从1到2,2到3,等等。
https://stackoverflow.com/questions/40503668
复制相似问题