Mean of every row in a double matrix without looping

1 回表示 (過去 30 日間)
Ms. Mat
Ms. Mat 2012 年 12 月 27 日
Is it possible to find mean and std dev. of every row in a huge matrix with double data and a lot of NaN interspersed without looping through every row ?
A = [1 2 NaN; 4 NaN 6 ; 8 9 10]
output = 1.5
5
9

採用された回答

Matt J
Matt J 2012 年 12 月 28 日
編集済み: Matt J 2012 年 12 月 29 日
If you have the Statistics toolbox, you can use nanmean(A,2) and nanstd(A,2).
If you don't have the toolbox, you can still avoid looping by doing
B=A;
map=~isnan(A);
B(~map)=0;
N=sum(map,2);
rowmeans = sum(B,2)./N;
rowstds = sqrt( sum(B.^2,2)./N -rowmeans.^2 );
If A is of type sparse, however, the above may need to be modified for efficiency's sake.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by