在matlab中,我说有以下数据:
data = [4 0.1; 6 0.5; 3 0.8; 2 1.4; 7 1.6; 12 1.8; 9 1.9; 1 2.3; 5 2.5; 5 2.6];
我想根据第2列中的元素(即0-1,1-2,2-3 ...)将第1列放入箱中,并计算元素的平均值和95%置信区间。该区域内的第1列。所以我有一个像这样的矩阵:
mean lower_95% upper_95% bin
4.33 0
7.5 1
3.67 2
答案 0 :(得分:2)
您可以将accumarray
与平均值(mean
)或分位数(quantile
)的相应函数一起使用:
m = accumarray(floor(data(:,2))+1, data(:,1), [], @mean);
l = accumarray(floor(data(:,2))+1, data(:,1), [], @(x) quantile(x,.05));
u = accumarray(floor(data(:,2))+1, data(:,1), [], @(x) quantile(x,.95));
result = [m l u (0:numel(m)-1).'];
这也可以使用单元格数组输出调用accumarray
一次:
result = accumarray(floor(data(:,2))+1, data(:,1), [],...
@(x) {[mean(x) quantile(x,.05) quantile(x,.95)]});
result = cell2mat(result);
对于您的示例数据,
result =
4.3333 3.0000 6.0000 0
7.5000 2.0000 12.0000 1.0000
3.6667 1.0000 5.0000 2.0000
答案 1 :(得分:1)
这将输出带有标记列的矩阵。请注意,对于您的示例数据,与均值的2个标准偏差(对于95% confidence interval)给出了波段之外的值。使用更大(正态分布)的数据集,您将看不到这一点。
您的数据:
data = [4 0.1; 6 0.5; 3 0.8; 2 1.4; 7 1.6; 12 1.8; 9 1.9; 1 2.3; 5 2.5; 5 2.6];
输出表的分区:
% Initialise output matrix. Columns:
% Mean, lower 95%, upper 95%, bin left, bin right
bins = [0 1; 1 2; 2 3];
out = zeros(size(bins,1),5);
% Cycle through bins
for ii = 1:size(bins,1)
% Store logical array of which elements fit in given bin
% You may want to include edge case for "greater than or equal to" leftmost bin.
% Alternatively you could make the left bin equal to "left bin - eps" = -eps
bin = data(:,2) > bins(ii,1) & data(:,2) <= bins(ii,2);
% Calculate mean, and mean +- 2*std deviation for confidence intervals
out(ii,1) = mean(data(bin,2));
out(ii,2) = out(ii,1) - 2*std(data(bin,2));
out(ii,3) = out(ii,1) + 2*std(data(bin,2));
end
% Append bins to the matrix
out(:,4:5) = bins;
输出:
out =
0.4667 -0.2357 1.1690 0 1.0000
1.6750 1.2315 2.1185 1.0000 2.0000
2.4667 2.1612 2.7722 2.0000 3.0000