Calculate standard deviation for different groups

15 ビュー (過去 30 日間)
leonidas86
leonidas86 2018 年 7 月 23 日
コメント済み: Guillaume 2018 年 7 月 23 日
I want to plot a Measurement with a bar figure. Therefore I want to create different groups and calculate the standard deviation for each group. I have a vector "Measurement" which is sorted according to increasing values. I get group IDs for the Measurement values by the use of the histc function.
[~,Measure_ID]=histc(Measurement_x,0:200:max);
In the next step I try to calculate the standard deviation with the accumarray function.
group_std = accumarray(Measure_ID,Measurement_y,[],@std);
The std command uses the follow mathematical formula:
The problem is that accumarray calculates the arithmetic average for each group separately. But I want to see the standard deviation regarding to the global arithmetic average. Is there a way to solve my issue?
Version: 2012b
  1 件のコメント
Jan
Jan 2018 年 7 月 23 日
編集済み: Jan 2018 年 7 月 23 日
Do you mean:
std(Measurement_y)
? What exactly is "standard deviation regarding to the global arithmetic average"?

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 7 月 23 日
編集済み: Jan 2018 年 7 月 23 日
If you want to set the x_bar values of the formula to mean(Measurement_y), why not implementing this as function?
function s = specialStd(x, m)
s = sqrt(sum((x - m) .^ 2) / (length(x) - 1));
end
and call it like:
m = mean(Measurement_x);
group_std = accumarray(Measure_ID, Measurement_y, [], @(x) specialStd(x, m));
  1 件のコメント
Guillaume
Guillaume 2018 年 7 月 23 日
specialStd, notexactlystd, I think we can agree that this thing shouldn't be called plain std!

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 7 月 23 日
"The problem is that accumarray calculates the arithmetic average for each group separately".
It's not accumarray doing that, it's std, because that the definition of the standard eviation.
To do what you want, you'll have to implement your own standard deviation calculation:
whole_mean = mean(Mesurement_y);
group_notexactlystd = accumarray(Measure_ID, Measurement_y, [], @(v) sum((v - whole_mean).^2)/(numel(v)-1));

カテゴリ

Help Center および File ExchangeSurfaces, Volumes, and Polygons についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by