MATLAB: How to calculate th standard deviation for a part of a matrix

matrixmeanstandard deviation

Hi all,
I have a large matrix of EMG data, with 2000 trials(2000×501) 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!

Best 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.
Related Question