matlab - Compute mean and standard deviation in 2-d matrix -


    x  y     1  1.2     1  2.3      1  4.5     2  2.3     2  1.2     2  0.8 

convert :

 x   ymean     ystandard-deviation  1    value       value 

how can can convert first matrix second?

simply use logical indexing extract out corresponding y values each unique x value, find mean , standard deviation of resulting y values.

specifically:

x = [1 1 1 2 2 2]; y = [1.2 2.3 4.5 2.3 1.2 0.8]; y1 = y(x == 1); y2 = y(x == 2);  m1 = mean(y1); s1 = std(y1); m2 = mean(y2); s2 = std(y2); 

we get:

>> m1  m1 =      2.6667  >> m2  m2 =      1.4333  >> s1  s1 =      1.6803  >> s2  s2 =      0.7767 

m1,m2 , s1,s2 means , standard deviations of y values corresponding x = 1 , x = 2 respectively.


in general, can use accumarray group of y values according each unique x value. way, can accommodate many unique values of x without having need use logical indexing each unique value of x.

in case x unsorted, can sort them first using unique use first output contains of unique x values , use third output reassigns each value of x unique id sorted. these used keys accumarray:

[vals, ~, id] = unique(x); m = accumarray(id, y, [], @mean); s = accumarray(id, y, [], @std); 

m , s contain mean , standard deviation each unique value of x. also, corresponding positions of m , s correspond same positions in vals.

let's had example instead:

x = [1 2 3 2 4 2 1]; y = [1.2 2.3 4.5 2.3 1.2 0.8 1.6]; 

if used above code, get:

>> vals  vals =       1     2     3     4  >> m  m =      1.4000     1.8000     4.5000     1.2000  >> s  s =      0.2828     0.8660          0          0 

don't alarmed last 2 entries having standard deviation of 0. that's definition when have data set consists of 1 point. there 1 point defined both x = 3 , x = 4.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -