How to calculate the standard deviation of all the elements of an array?
The std function divides by column. How do you calculate for all elements?
How to calculate the standard deviation of all the elements of an array?
The std function divides by column. How do you calculate for all elements?
Given a a
array, you can always access all elements using a(:)
.
Example:
a=magic(3)
a =
8 1 6
3 5 7
4 9 2
a(:)
ans =
8
3
4
1
5
9
6
7
2
In your case, just use std(a(:))
or instead of std(a)
.
soma = 0;
soma = a(:,:) + soma; %Basta informar as dimensões da matriz
n = size(a);
media = (soma / n);
s = 0;
% a fórmula de desvio padrão deve ser conferida e testada.
for i = 1: n
s = (( a(x) - media )^2)
end
s = ( ( s / (n-1) ) ^ 0.5 )