Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Mean of every row in a double matrix without looping

Asked by Ms. Mat

Ms. Mat

on 27 Dec 2012
Accepted Answer by Matt J

Matt J

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


Ms. Mat

Ms. Mat


No products are associated with this question.

1 Answer

Answer by Matt J

Matt J

on 28 Dec 2012
Edited by Matt J

Matt J

on 29 Dec 2012
Accepted answer

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

 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.

1 Comment

Ms. Mat

Ms. Mat

on 29 Dec 2012

Thank You !!!

Matt J

Matt J

Contact us