Info
This question is closed. Reopen it to edit or answer.
Creating a vector to list values occurring within 5 seconds before a designated time
5 views (last 30 days)
Show older comments
I have the following data:
buzz time click time
16948 16942.58755
16966.2 16942.77004
17048.7 16942.95203
17090.5 16943.14491
17104.4 16943.32836
17111.8 16943.51334
17138.1 16943.69869
17178.3 16943.88338
16944.06336
16944.23747
16944.42053
16944.60682
16944.78904
16944.97181
16945.15974
16945.35162
16945.53905
16945.72622
16945.91283
16946.1051
16946.28659
16946.46027
16946.62662
16946.80914
16946.99274
16947.18359
16947.37839
16947.56643
16947.75977
16951.8645
16952.04876
16952.24642
16952.42928
16952.62402
16952.81478
16953.00297
16953.19304
16953.38403
16953.58685
16953.7825
16953.97595
16954.17543
16954.38145
16954.57069
16954.75829
16954.95385
16955.15018
16955.36247
16955.5849
16955.81282
16956.03784
16956.24667
16956.45085
16956.66187
16956.88283
16957.09311
16957.30488
16957.51525
16957.71652
16957.9289
16958.12451
16958.33015
16958.53322
16958.74688
16958.92939
16959.12829
16959.38434
16959.57244
16959.77432
16962.28846
16962.49168
16962.69591
16962.89133
16963.10165
16963.28117
16963.44878
16963.62042
16963.7931
16963.9592
16964.18957
16964.37206
16964.56041
16964.75655
16964.94539
16965.12754
16965.31854
16965.49747
16965.68268
16965.8561
16966.03613
Given this data, I am trying to find a code that will allow me to create a vector with each row containing all of the click times occurring within 5 seconds before the buzz time for each buzz. For example, if the data above were to be used, I would see an array with 8 rows (since there are 8 buzzes in this example), with each of the rows containing the values for the click times within 5 seconds before the corresponding buzz time. Is this possible to do in MatLab? Thanks for your help!
2 Comments
Answers (1)
Kelly Kearney
on 6 Jul 2016
In your example, the number of clicks corresponding to a particular buzz is not the same for all buzzes, so storing as rows of a matrix isn't possible (unless you pad out some of the rows with placeholders). A cell array would be a better choice. Assuming buzz and click are both column vectors:
isclose = num2cell(abs(bsxfun(@minus, buzz', click)) < 5, 1);
closeclicks = cellfun(@(mask) click(mask), isclose, 'uni', 0);
2 Comments
Kelly Kearney
on 12 Jul 2016
Sorry, misread your question as within 5 seconds rather than only 5 secs before. Not sure why you're seeing correct answers for the closeclicks array as defined above, though
buzz = [20 30]';
click = rand(50,1)*100;
dt = bsxfun(@minus, buzz', click);
% Within 5 secs
isclose = num2cell(abs(dt) <= 5, 1);
closeclicks1 = cellfun(@(mask) click(mask), isclose, 'uni', 0);
% 5 secs before only
isclose = num2cell(dt >= 0 & dt <= 5, 1);
closeclicks2 = cellfun(@(mask) click(mask), isclose, 'uni', 0);
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!