How to calculate th standard deviation for a part of a matrix?

Hi all,
I have a large matrix of EMG data, with 2000 trials(2000x501) for 10 subjects. I need to calculate the mean and std for certain epochs of this data. Here an example of what I need to do:
%%calculate mean activity for pre, R1, R2, R3 and vol
j=find(data.DotsDirection==-1 & data.CorrectTarget(:)==1);
% 1st muscle
m1pre=mean(mean((data.emg1(j,1:129)),2)); % mean emg for pre epoch for all trials
m1r1=mean(mean((data.emg1(j,129:151)),2));
m1r2=mean(mean((data.emg1(j,151:184)),2));
So with the first step I select only certain trials, with the second step I calculate the mean per epoch. As you see I first calculated the mean per epoch per trial and then took the mean of this again (as I need one value for the mean and std). I need to do the same for the std but I'm wondering if I'm actually allowed to do this? Or is there a function that allows me to take the mean/std per epoch in one step?
Any help appreciated!

3 Comments

Jana, is it possible you hang the data file, or part of it, so readers can have a look?
John
Hi John, I attached data with the first 500 trials. Hope that helps!
You can easily write your own function to calculate both mean and standard deviation based on that mean.

Sign in to comment.

 Accepted Answer

The standard deviation of the standard deviation of the rows will not be the same as the standard deviation of the whole matrix. So no, you cannot do std(std(x)). The simplest way to calculate the standard deviation (and the mean) is to reshape the input into a vector. The normal way this is done:
%mean and standard deviation of n-dimensional x:
mean(x(:)) %x(:) reshape into a column vector
std(x(:))
However, you cannot chain the (:) indexing with your indexing, so you can either invoke reshape explicity:
mean(reshape(data.emg1(j,1:129), 1, []))
std(reshape(data.emg1(j,1:129), 1, []))
Or use a helper function:
flatten =@(x) x(:);
mean(flatten(data.emg1(j,1:129)))
std(flatten(data.emg1(j,1:129)))
Also note that the find is totally unnecessary and only slows down your code. If you do:
j = data.DotsDirection == -1 & data.CorrectTarget(:) == 1; %j is now a logical array
mean(reshape(data.emg1(j, 1:129), 1, []))
std(reshape(data.emg1(j, 1:129), 1, []))
it will work just as well. See the help on logical indexing.

2 Comments

Thank you for the solution and explanation, works perfectly! It makes sense. However, it should not make a difference for taking the mean, right? Taking the mean per trial and then again the mean of all trials should be the same as mean(x(:)), or am I totally off?
Also thanks for the tip with the indexing, I didn't know the find function could slow down my code.
Yes, taking the mean twice is valid. It's probably slower than reshaping the matrix into a vector though.
Calling a function that's not needed is bound to be slower than not calling it all. Saying that, the difference in time with find is probably so small you won't notice.

Sign in to comment.

More Answers (0)

Asked:

on 8 Feb 2016

Commented:

on 8 Feb 2016

Community Treasure Hunt

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

Start Hunting!