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!