Trying to find the mean and standard deviation without using the built in functions?
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Olivia Colombo
am 12 Feb. 2019
Beantwortet: John D'Errico
am 12 Feb. 2019
I'm new to functions so any help would be appreciated!
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 12 Feb. 2019
You show a function that does it already, although at a quick glance, it appears to compute the standard deviation incorrectly. Is your question how to do it correctly?
First, I would NOT use std as the name of a variable. One day you will want to use the true function std, and you won't be able to, and then you will come back crying about a strange bug. Get used to not naming variables like that.
Some parts of your code were ok. But you already know how to use sum. So use it!
function avgandstd(x)
x = x(~isnan(x));
n = length(x);
avg = sum(x)/n;
S = sqrt(sum((x - avg).^2/(n-1)));
disp(['mean = ',num2str(avg),' and standard deviation = ',num2str(S)])
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!