MATLAB: How to add components of a vector given conditions from another vector of different dimensions

conditional functionsum

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!

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