Hello, I would like to convert this mathematical equation into MATLAB code.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
![Hello, I would like to convert this mathematical equation into MATLAB code](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1239312/image.png)
1 Kommentar
John D'Errico
am 21 Dez. 2022
You already got a valid answer. There is no reason to then post the same question a second time. I closed the other.
Antworten (1)
Bora Eryilmaz
am 21 Dez. 2022
Bearbeitet: Bora Eryilmaz
am 21 Dez. 2022
x = rand(100,1);
N = numel(x);
num = (sum(x)/N)^2;
den = sum(abs(x.^2)) / N;
s = num / den
1 Kommentar
Walter Roberson
am 21 Dez. 2022
x_m = rand(100,1);
%Bora's version, valid only for reals
N = numel(x_m);
num = (sum(x_m)/N)^2;
den = sum(abs(x_m.^2)) / N;
s = num / den
%a more compact version valid only for reals
num2 = mean(x_m).^2;
den2 = mean(x_m.^2);
s2 = num2/den2
%analog for compact version but valid for complex as well
num3 = abs(mean(x_m)).^2;
den3 = mean(abs(x_m).^2);
s3 = num3 / den3
What you can get away with depends on whether there might be complex numbers involved.
Siehe auch
Kategorien
Mehr zu Function Creation 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!