How to compute the standard deviation of each column in a matrix?

33 次查看(过去 30 天)
I have some code that creates a matrix of data points and then computes the average value for each column and plots these average values. However, I want to add some error bars to my graph where each error bar represents the standard deviation across each column entry in the original matrix. Is there a concise way to compute the standard deviation for each column so that I can store it and use these standard deviations to make a plot (using something like 'errorbar(x, y, err)'? Thanks!

回答(2 个)

the cyclist
the cyclist 2021-8-27
编辑:the cyclist 2021-8-27
If A is your array, then
s = std(A)
is a row vector, where each element is the standard deviation of the corresponding column. See the documenation for std for details.
Also, be sure to be clear about whether your error bars represent the standard deviation or the standard error of the mean.
  2 个评论
Victoria Helm
Victoria Helm 2021-8-28
Thanks so much this is super helpful! I decided I also want to try plotting with the SEM for my error bars. I understand that I will have to divide the standard deviation by the square root of the number of elements in each column. However, some of my column elements are NaN so I used std(A, 'omitnan') initially. Is it possible to compute SEM and use 'omitnan'? (I don't know ahead of time how many empty data points I have in each column.
the cyclist
the cyclist 2021-8-29
You could use the isnan function to keep track of how many elements are NaN. So, calculate the standard deviation, and divide by the square root of the number of elements that are not NaN.

请先登录,再进行评论。


Steven Lord
Steven Lord 2021-8-28
To be certain that you're taking the std over each column, specify the dim input argument as described on the documentation page for the std function.

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by