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!!

 Respuesta aceptada

Star Strider
Star Strider el 5 de Mzo. de 2018
Editada: MathWorks Support Team el 2 de Mzo. de 2023

5 votos

UPDATE: Starting in R2022b, you can now calculate Root Mean Square Error using the built in MATLAB function ‘rmse’:
*************************************************************
‘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 comentarios

silvia battistella
silvia battistella el 6 de Mzo. de 2018
thank you so much!!!
Star Strider
Star Strider el 6 de Mzo. de 2018
As always, my pleasure!
Abinaya G EMBDD
Abinaya G EMBDD el 21 de Ag. de 2019
what is v1 and v2
Star Strider
Star Strider el 21 de Ag. de 2019
Anything you want them to be!
MAT-Magic
MAT-Magic el 18 de En. de 2020
Editada: MAT-Magic el 18 de En. de 2020
@ Dear Star Strider can we use the below formula for V1 and V2?
difference = single(1) - single(2);
squaredError = difference .^ 2;
meanSquaredError = sum(squaredError(:)) / numel(signal(1));
rmsError = sqrt(meanSquaredError);
Thanks
Star Strider
Star Strider el 18 de En. de 2020
That appears to be the correct way to calculate RMSE.
MAT-Magic
MAT-Magic el 18 de En. de 2020
Thanks. I edited the question and removed the mistakes.
Star Strider
Star Strider el 18 de En. de 2020
My pleasure.
I assume ‘single(1)’ and ‘single(2)’ are actually equal-length vectors.
Image Analyst
Image Analyst el 18 de En. de 2020
There is an rms() function that you can use. It's been in the Signal Processing Toolbox since R2012a.
MAT-Magic
MAT-Magic el 19 de En. de 2020
@Star Yes, it's right.
@Image Thanks for the reply.
Rachel Hall
Rachel Hall el 24 de Abr. de 2020
@Image Analyst,
I don't think RMSE and RMS are the same; I'm currently fining that out...
Marcos Conde
Marcos Conde el 26 de En. de 2022
Yes and No. Actually, RMSE=rms(E), since E(rror) is the difference. In the example above: RMSE=rms(V1-V2)

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 5 de Mzo. de 2018

3 votos

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.

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by