standard deviation of all elements of a matrix in MATLAB?

Asked

Viewed 619 times

0

How to calculate the standard deviation of all elements of a matrix?

the Std function divides by column. How to calculate by all elements?

2 answers

1

Given a matrix a, you can always access all the 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).

1

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 ) 
  • Hello, could you clarify a little better your methodology?

  • Hello Wellington, besides a code, an explanation (even if brief) of what your code does helps the owner of the question, as well as other users who might have the same problem or something similar that this question might solve. Also, your code does not work with your example. n is a matrix, if soma is just the sum of all points (what your code does not show), this returns an error (Matrix Dimensions must agree).

  • 1

    Note that dividing by (n-1) is the sample standard deviation. If the original matrix contained all possible equiprovable results of an experiment it should be divided by n. The original question is unclear as to this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.