# RMSE between two variables

800 views (last 30 days)
silvia battistella on 5 Mar 2018
Commented: Marcos Conde on 26 Jan 2022
Hello, I am really new at matlab. I am trying to create a sub-function that has an input of two vectors and output the RMSE between the values in the vectors. Anyone can help? I would love to understand step by step. thanks to anybody that can help!!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 5 Mar 2018
‘RMSE’ of course means ‘root mean squared error’, or the square root of the mean of the squared error.
The simplest code for this is then:
V1 = rand(10,1);
V2 = rand(10,1);
RMSE = sqrt(mean((V1-V2).^2));
where the error is (V1-V2), and ‘.^2’ denotes element-wise squaring of the error (the difference between ‘V1’ and ‘V2’). The rest of the expression takes the mean of the squared differences, and sqrt takes the square root, completing the definition.
See: Array vs. Matrix Operations (link) and Vectorization (link) for more information.
##### 12 CommentsShowHide 11 older comments
Marcos Conde on 26 Jan 2022
Yes and No. Actually, RMSE=rms(E), since E(rror) is the difference. In the example above: RMSE=rms(V1-V2)

Sign in to comment.

### More Answers (1)

Rik on 5 Mar 2018
To learn Matlab, you can use a guide like this one.
There are two main ways of doing this: an anonymous function and a 'normal' function.
%anonymous function:
calculate_RMSE=@(a,b) sqrt(mean((a(:)-b(:)).^2));
%normal function (save this in calc_RMSE.m)
function rmse=calc_RMSE(a,b)
rmse=sqrt(mean((a(:)-b(:)).^2));
The two function can be used in the exact same way. The second option provides more options for checking if the input is correct.
##### 1 CommentShowHide None
silvia battistella on 6 Mar 2018
thank you so much!! it really helped me!

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!