View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.3 | 6 ratings Rate this file 76 Downloads (last 30 days) File Size: 466 Bytes File ID: #21383 Version: 1.1



Felix Hebeler (view profile)


09 Sep 2008 (Updated )

calculates root mean square error from data vector or matrix and the corresponding estimates.

| Watch this File

File Information

Short script that calculates root mean square error from data vector or matrix and the corresponding estimates.
Checks for NaNs in data and estimates and deletes them and then simply does:
r = sqrt( sum( (data(:)-estimate(:)).^2) / numel(data) );
That's it.


This file inspired Rmse(True Values, Prediction).

MATLAB release MATLAB 7.2 (R2006a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
22 Feb 2016 ozge

ozge (view profile)

14 Dec 2015 Du

Du (view profile)

20 May 2015 Ruize Lee  
25 Apr 2014 ADABA Edem  
12 Jun 2011 Hassan Naseri

I always use mean function instead of sum and divide
rms = sqrt(mean((data(:)-estimate(:)).^2));

Comment only
08 Mar 2010 Andre Guy Tranquille  
27 Oct 2008 Wolfgang Schwanghart

Hi Felix and Gary,

yes, the two sums could be avoided by simply writing

The computation time is about the same but readability might be enhanced by using the colon operator.

Best regards,

Comment only
10 Oct 2008 Felix Hebeler

@Gary: no, you need two sums if you process matrices, the first sums across all columns, the second then sums across the resulting vector. If you process vectors, the second sum calculates the sum of a scalar. Faster than checking for dimensions first.

Comment only
09 Oct 2008 Gary Merkoske

you have one too many SUM() in the eqn, although it appears to be harmless. Am I correct? RMS Error is then;

11 Sep 2008 Felix Hebeler

Thanks for the feedback Wolfgang, I completely forgot that nansum needs the statistical toolbox, and of course you are right that it becomes incorrect with nans. I should have divided by numel(~isnan(data)), but deleting all NaNs in this case _is_ better! Your version actually would extract all NaNs and discard the values, so I used
I = ~isnan(data) & ~isnan(estimate); instead, which works a treat!

Durga, it's great you advertise your script on my page ;-) I see no point in input argument checking for this oneliner though - in my case I would have to reshape my matrices to use your script, not sure if that is better...

Anyway, once your script takes care of NaNs as suggested by Wolfgang, it is surely great as it calculates more than one goodness of fit.

Comment only
10 Sep 2008 Wolfgang Schwanghart

Hi Felix,

the formula becomes incorrect as soon as you have nans in your arrays. You should remove nans first in both arrays

I = isnan(data) | isnan(estimate);
data = data(I);
estimate = estimate(I);

and then apply the formula. That even allows you to use sum instead of nansum, thereby avoiding dependence on the statistical toolbox.

Comment only
09 Sep 2008 Durga Shrestha

This code is without input argument checking.

To compute more types of goodness of fit (including RMSE, coefficient of determination, mean absolute relative error etc.) please have a look

Comment only
11 Sep 2008

include NaN checking

11 Sep 2008

- delete NaNs and use sum instead of nansum, eliminating the need for the statistical toolbox

27 Nov 2008 1.1

Updated description and code for better readability and

31 Mar 2016 1.1

BSD update

Contact us