Am I doing this right? (a Math formula to Matlab language)
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I needed to calculate this formula:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271805/image.jpeg)
So I wrote a function in order to do that:
(P is x and P^ is y):
function NS = nash_sat(x, y)
% This function calculates Nash-Sutcliffe efficiency coefficient
% Detailed explanation goes here
A = sum (x-y).^2;
B = sum (x-(mean(x)).^2;
fraction = (A/B);
NS = 1-fraction;
end
This is the first time I type a math formula in Matlab. I want to know if I typed the formula in the function correctly or not.
Thank you so much.
0 Kommentare
Akzeptierte Antwort
James Tursa
am 15 Feb. 2020
Bearbeitet: James Tursa
am 15 Feb. 2020
You are on the right track, but have typos in the code due to parentheses issues.
A = sum( (x - y).^2 );
B = sum( (x - mean(x)).^2 );
Weitere Antworten (2)
Giuseppe Inghilterra
am 15 Feb. 2020
Hi,
A and B terms are wrong.
What you wrote is the square of sum of differences. Instead formula is the sum of square of differences.
The code should be written as follow:
A = sum((x-y).^2);
B = sum((x-(mean(x))).^2);
fraction = (A/B);
NS = 1-fraction;
Pay attention also to space left between "sum" and "(x-y)" in your code: it is not correct.
0 Kommentare
akca -
am 26 Dez. 2024
Bearbeitet: akca -
am 26 Dez. 2024
function NS = nash_sat1(x, y)
% This function calculates Nash-Sutcliffe efficiency coefficient
% Detailed explanation goes here
A = sum((minus(x,y)).^2);
B = sum((minus(x,(mean(x))).^2));
fraction = (A./B); %Element-Wise
NS = 1-fraction;
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!