MATLAB Answers

Split arrays and Apply function

1 view (last 30 days)
Ana Gabriela Guedes
Ana Gabriela Guedes on 19 Apr 2021
Edited: Walter Roberson on 19 Apr 2021
Hi! I have 4 vectors:
event1 = [7.046, 15.66, 24.16, (....) 106.672, 114.722]
event2 = [8.82, 17.34, 25.91, 33.07, (....) 108.25, 116.30]
event3 = [9.05, 17.63, 26.18, (.....) 108.55, 116.55]
correspond to the instants in which 3 different events occured
B = [0.53, 1.25, 2.01, 2.78, 3.51, 4.25, 4.96, 5.78, 6.55, 7.26, 7.98, 8.76, 9.54, 10.32,11.08, 11.77, 12.43, 13.68, 14.77, 15.57, 16.31( ...) 115.73, 116.57]
that corresponds to the instants in which event B is happening. I want to separate the difference between each B value occuring between event3 and event1 and save each interval in a cell array.
interval3_1 = {}: event3(i) < B < event1(i+1) (after event 3 and before 1)
For example I would have:
[B(2)-B(1) B(3)-B(2) ... B(9) - B(8)]
(since B(9) = 6.55 < event(1)= 7,046) in the first position of cell array interval 3_1 and
[B(14) - B(13) ... B(21) - B(20)]
(because B(13) = 9.53 > event3(1) and B(21) = 15.57 < event1(2) ) in the second position on cell array interal3_1. I tried to do this inside ifs and for cicles but but it's not working, maybe there is defined functions that can help me How can I do it?

Answers (1)

Walter Roberson
Walter Roberson on 19 Apr 2021
event1 = [7.046, 15.66, 24.16]
event1 = 1×3
7.0460 15.6600 24.1600
B = [0.53, 1.25, 2.01, 2.78, 3.51, 4.25, 4.96, 5.78, 6.55, 7.26, 7.98, 8.76, 9.54, 10.32,11.08, 11.77, 12.43, 13.68, 14.77, 15.57, 16.31]
B = 1×21
0.5300 1.2500 2.0100 2.7800 3.5100 4.2500 4.9600 5.7800 6.5500 7.2600 7.9800 8.7600 9.5400 10.3200 11.0800 11.7700 12.4300 13.6800 14.7700 15.5700 16.3100
Bt = [-inf, B, inf];
bins = discretize(event1, Bt)
bins = 1×3
10 21 22
output = Bt(bins+1) - Bt(bins)
output = 1×3
0.7100 0.7400 Inf
Inf results correspond to event1 inputs that are not within the range of B
  2 Comments
Walter Roberson
Walter Roberson on 19 Apr 2021
Also, I think I dont exactly understand what inf is supposed to correspond to
What do you want the output to be if you have event data that has values that is not within any pair of consecutive B values? For example suppose that one of the events has data 0.5, which is before your first B value ?
The sample data you posted does not have that issue, but I presume that your data could change.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!