Calculating Means for Blocks of Data from Excel

2 views (last 30 days)
I have some EMG data that I have processed and now have in an Excel file.
Within the excel file I have used an IF statement to identify whether the signal is above a set threshold in which case it is of interest. I would then like to take the mean for each block of data that the signal is active.
Probably eassiest to explain this with an exapmle (numbers selected to make exaple simple to understand):
Let's say that I had the following data set:
2 1 2 3 5 8 6 9 4 3 2 3 2 1 4 6 7 8 9 6 3 2
Now let's say that the threshold for activity is greater than or equal to 4, (If statement in excel provides 0, but could be false or blank), so I would get:
0 0 0 0 5 8 6 9 4 0 0 0 0 0 4 6 7 8 9 6 0 0
I would like to know the avaerge of the two active periods.
i.e. mean [ 5 8 6 9 4] and [4 6 7 8 9 6], is this possible with Matlab and if so, how would I go about it?
Any help would be greatly appreciated.

Answers (1)

Andrei Bobrov
Andrei Bobrov on 22 Jun 2019
Edited: Andrei Bobrov on 22 Jun 2019
A = [2 1 2 3 5 8 6 9 4 3 2 3 2 1 4 6 7 8 9 6 3 2];
lo = A(:) >= 4;
loo = cumsum([0;diff(lo)==1]).*lo;
lo3 = loo > 0;
out = accumarray(loo(lo3),A(lo3),[],@mean);
  1 Comment
Julian Dale
Julian Dale on 22 Jun 2019
Hi Andrei,
That's great.
Thank you for the quick response and for your help.
Julian

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!