Standard deviation of all matrix elements (single value)

I have a random matrix X and I am looking for the standard deviation considering all elements; however I am asked (by a text book, learning Matlab) not to use loops. The result should be one single value.
std(X) gives a matrix of standard deviation of all columns which is not what I am looking for. (something corresponding to m=mean(mean(X)) it is not working for the standard deviation)
PS: sorry for asking this simple question; I did some search on this topic but couldn't find useful hints.
Thank you for your help!

回答 (3 件)

Adam
Adam 2015 年 3 月 2 日
編集済み: Adam 2015 年 3 月 2 日

4 投票

std( X(:) )
should give you the standard deviation of all elements in a matrix, X, of any shape.
This is also generally better for mean since it works for any dimensionality rather than mean( mean( X ) ) which is hard-coded as 2d.

1 件のコメント

Adam
Adam 2015 年 3 月 2 日
Please add a comment to an answer rather than a new answer (it is a mistake many make though, not just you!).
The syntax
X(:)
simply collapses your array into a column so that the std command (or any other similar command) will produce a single answer as it would on a normal column array. For statistics where the 2d or n-d structure of the data is irrelevant this works well. Obviously it needs more manipulation for functions where the 2d structure matters.

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

Torsten
Torsten 2015 年 3 月 2 日

0 投票

Use "reshape" to get an (N*M,1) column vector from the (N,M)-matrix.
Best wishes
Torsten.
Joachim Schmid
Joachim Schmid 2015 年 3 月 2 日

0 投票

Thank you very much Torsten and Adam! Adam, now I have to understand the command.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2015 年 3 月 2 日

コメント済み:

2015 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by