How to create a function that will output sample std when given an array as an input?

1 Ansicht (letzte 30 Tage)
Hi all, I'm working on a project for my class that has me writing a function that does what the title says. We aren't allowed to use the built-in std function matlab already provides. I have the skeleton of the code––the function works for column and row vectors but not matrices––and now I'm at a loss for what I'm doing wrong:
I'm also not allowed to use the built-in mean function, but was able to write my own mean function (M) that works the same way. Any help would be appreciated. Thanks!

Antworten (1)

Walter Roberson
Walter Roberson am 12 Mär. 2023
for n=1:c
M=sum(x(:,n))./b;
end
Every iteration of the for n loop, you are overwriting all of the array M. You should be storing the result indexed by n
for i=1:k
why are you using two different variables, c and k to both represent the same meaning, the number of columns ?
stdev=sqrt(sum((x(i)-M).^2./(k-1)))
you are overwriting all of stdev each iteration.
You are indexing x with one index, whereas before you were indexing it with two indexes.
You calculated the mean for each column, but now you are dividing by the number of columns, rather than by the number of rows.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by