How can I add components of a vector given conditions from another vector of different dimensions?

I have the following columns of data:
buzz length start cue end cue
803 0.7 7 392.6
810.9 0.9 502.4 1002.4
1514.8 0.4 1231.6 1851.4
1516.2 0.9 2226 2782.2
1753 0.8 4024.2 4520
2470.6 2.9 5050.2 5536.2
4278.1 1.3 6059.2 6699.8
4415.9 2.9
5361.9 1.1
6244.1 5.7
6446.6 1.2
6459 2.7
I am trying to create a code that, like the SUMIFS function in Excel, will take the number of buzzes who's time (column 1) falls within the range of a start/end cue pair (columns 3 and 4) and sum their respective lengths (column 2). I'm sure the solution has something to do with indexing to refer to the index positions of some of the values, however I am not sure how to even approach this problem. Any help is appreciated! Thank you for your time!

 Accepted Answer

This is fast and easy to solve by using a simple mtimes.
Your data (as column vectors):
>> cueBeg = [7;502.4;1231.6;2226;4024.2;5050.2;6059.2];
>> cueEnd = [392.6;1002.4;1851.4;2782.2;4520;5536.2;6699.8];
>> datLen = [0.7;0.9;0.4;0.9;0.8;2.9;1.3;2.9;1.1;5.7;1.2;2.7];
>> datBuz = [803;810.9;1514.8;1516.2;1753;2470.6;4278.1;4415.9;5361.9;6244.1;6446.6;6459];
and the calculation:
>> datLen.' * (bsxfun(@gt,datBuz,cueBeg.')&bsxfun(@lt,datBuz,cueEnd.'))
ans =
0.00000 1.60000 2.10000 2.90000 4.20000 1.10000 9.60000
The output vector gives the sum of all "lengths" between each pair of cues. You might prefer to use ge and le instead of gt and lt.

4 Comments

I just tried out the code, it worked! Thank you so much! One more question: is there a way that the code can be modified to count the number of buzzes occurring within these intervals?
Please see my edited answer and remember to accept the answer that best solves your problem.
@Mariana Kneppers: Of course:
>> cueIdx = bsxfun(@gt,datBuz,cueBeg.') & bsxfun(@lt,datBuz,cueEnd.');
>> datLen.' * cueIdx % the total time length in each interval
ans =
0 1.6 2.1 2.9 4.2 1.1 9.6
>> sum(cueIdx,1) % the number of buzzes in each interval
ans =
0 2 3 1 2 1 3
perfect, thank you so much to you both! I really appreciate the help!

Sign in to comment.

More Answers (1)

Shamelessly plagiarizing Stephen's data reconstruction, but providing a one liner instead:
cueBeg = [7;502.4;1231.6;2226;4024.2;5050.2;6059.2];
cueEnd = [392.6;1002.4;1851.4;2782.2;4520;5536.2;6699.8];
datLen = [0.7;0.9;0.4;0.9;0.8;2.9;1.3;2.9;1.1;5.7;1.2;2.7];
datBuz = [803;810.9;1514.8;1516.2;1753;2470.6;4278.1;4415.9;5361.9;6244.1;6446.6;6459];
result = arrayfun(@(x,y) sum(datLen(datBuz>x & datBuz < y)),cueBeg,cueEnd)
EDIT
and to get the count:
counts = arrayfun(@(x,y) sum(datBuz>x & datBuz < y),cueBeg,cueEnd)

Community Treasure Hunt

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

Start Hunting!