How to calculate th standard deviation for a part of a matrix?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
JanaM
am 8 Feb. 2016
Kommentiert: Guillaume
am 8 Feb. 2016
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 Kommentare
Adam
am 8 Feb. 2016
You can easily write your own function to calculate both mean and standard deviation based on that mean.
Akzeptierte Antwort
Guillaume
am 8 Feb. 2016
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, []))
2 Kommentare
Guillaume
am 8 Feb. 2016
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!