How to create a function that will output sample std when given an array as an input?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuestas (1)
Walter Roberson
el 12 de Mzo. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!