Split arrays and Apply function
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Walter Roberson
2021-4-19
event1 = [7.046, 15.66, 24.16]
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]
Bt = [-inf, B, inf];
bins = discretize(event1, Bt)
output = Bt(bins+1) - Bt(bins)
Inf results correspond to event1 inputs that are not within the range of B
2 个评论
Walter Roberson
2021-4-19
编辑:Walter Roberson
2021-4-19
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.
Felipe Padua
2022-9-13
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
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
disp(C)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!