How can I add components of a vector given conditions from another vector of different dimensions?
Show older comments
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
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)
Categories
Find more on Common Operations 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!