Split arrays and Apply function

1 view (last 30 days)
Ana Gabriela Guedes
Ana Gabriela Guedes on 19 Apr 2021
Answered: Felipe Padua on 13 Sep 2022
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 (2)

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
Ana Gabriela Guedes
Ana Gabriela Guedes on 19 Apr 2021
Thank you! but I dont think this solves my problem.
But this will olnly save the differences for the B values within one interval, right? And the intervals in which I want separate B and do B(i+1)-B(i) dont have all the same length and are between a value in vector event1 and a value in vector event2
Also, I think I dont exactly understand what inf is supposed to correspond to
Walter Roberson
Walter Roberson on 19 Apr 2021
Edited: 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.


Felipe Padua
Felipe Padua on 13 Sep 2022
First of all, something that probably will help you is the fact that vectors (and matrices and arrays) can be indexed not only by numerical values, but also by logical arrays of compatible size. This fact enables you use implicit expantion to resolve your problem.
First,
event1 = [7.046, 15.66];
event2 = [8.82, 17.34];
event3 = [9.05, 17.63];
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];
do the comparisons:
idx1 = B < [event1, inf]'; % the result is a logical matrix with size [length(event1)+1, length(B)]
idx3 = B > [0, event3]' ; % the result is a logical matrix with size [length(event3)+1, length(B)]
idx = idx1 & idx3 % I'm supposing here that event1 and event3 have same length
idx = 3×23 logical array
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
Next, perform a loop that collect the occurrences of B, calculate the intervals and store them:
n = size(idx, 1);
C = cell(n, 1);
for i = 1:n
idxi = idx(i, :);
Bi = B(idxi);
Ci = diff(Bi);
C{i} = Ci;
end
Then, show the results:
disp(Bi) % the values of B collected in the last iteration
115.7300 116.5700
disp(C)
{[0.7200 0.7600 0.7700 0.7300 0.7400 0.7100 0.8200 0.7700]} {[ 0.7800 0.7600 0.6900 0.6600 1.2500 1.0900 0.8000]} {[ 0.8400]}

Community Treasure Hunt

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

Start Hunting!