How do I extract means for a column based on multiple groups of rows in another column

2 次查看(过去 30 天)
Hello,
I have a 800000x14 numerical matrix. In column2 I have several values of importance recurring (e.g: data(:,2) == 1.0720). I have several other such recurring numbers of importance(n=18) in this column, these numbers represent the onset and offset of stimulus. In another column=7, I have the values which I want to average for the 20 rows before and after each recurring numbers, respectively, to extract the pre-onset/offset and post_onset/offset means.
How do I tell matlab to select and then average these 20 rows before and after these onset and offset values? And how can I create a loop which can automatically detect the different stimuli onsets/offsets and avg 20 rows before and after each of these points zhere each of these stim onset/ offsets have their distinct labels (for eg: B1_pre/post_onset/offset, B2_pre/post_onset/offset, B3_pre/post_onset/offset, etc.)
I am not used to creating loops in matlab, your help will be deeply appreciated.
  1 个评论
Tanaya Chatterjee
Tanaya Chatterjee 2022-10-26
I tried using the following code for the output variables.My data (Fl_ERSP_mu) has total 9 blocks where I need 4 parameters for each block. I have shared the first two blocks for your reference. I am looking for a loop which can carry out this function for each block. Also, using this script I am unable to gerenate the average variables in the workspace. Kindly assist.
m=Fl_ERSP_mu(:,2) == 1.0720 %block1onset
n=Fl_ERSP_mu(:,2) == 1.8880 %block1offset
o= Fl_ERSP_mu(:,2) == 2.2880 %block2onset
p= Fl_ERSP_mu(:,2) == 3.6800 %block2offset
if Fl_ERSP_mu(:,2) == 1.0720
B1_preOn= range (Fl_ERSP_mu(m-20:m,:));
B1_preOn_avg= mean (B1_preOn,"all") %mean of B1_preonset
B1_postOn= range (Fl_ERSP_mu(m:m+20,:));
B1_postOn_avg= mean (B1_postOn,"all") %mean of B1_postonset
elseif Fl_ERSP_mu(:,2) == 1.8880
B1_preOff= range (Fl_ERSP_mu(n-20:n,:))
B1_preOff_avg= mean (B1_preOff,"all") %mean of B1_preoffset
B1_postOff= range (Fl_ERSP_mu(n:n+20,:));
B1_postoff_avg= mean (B1_postOff,"all") %mean of B1_postoffset
elseif Fl_ERSP_mu(:,2) == 2.2880
B2_preOn= range (Fl_ERSP_mu(o-20:o,:));
B2_preOn_avg= mean (B2_preOn,"all") %mean of B2_preonset
B2_postOn= range (Fl_ERSP_mu(o:o+20,:));
B2_postOn_avg= mean (B2_postOn,"all") %mean of B2_postonset
elseif Fl_ERSP_mu(:,2) == 3.6800
B2_preOff= range (Fl_ERSP_mu(p-20:p,:));
B2_preOff_avg= mean (B2_preOff,"all") %mean of B2_preoffset
B2_postOff= range (Fl_ERSP_mu(p:p+20,:));
B2_postoff_avg= mean (B2_postOff,"all") %mean of B2_postoffset
end

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2022-10-26
编辑:Matt J 2022-10-26
For such small data sizes, I would splurge and just compute look-up tables of all the moving averages that could possibly be required.
averagesPre=movmean(data(:,7),[20,0]); %lookup table
averagesPost=movmean(data(:,7),[0,20]); %lookup table
meanOnsets = averagesPost(ismember( data(:,2) , onsetValues) ); %post-onset means
meanOffsets = averagesPre(ismember( data(:,2) , offsetValues) ); %pre-offset means

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by