How to reference a cell in a matrix only if it is preceded by a specific number?
1 次查看(过去 30 天)
显示 更早的评论
I have a 588x2 matrix. The first column are times associated with an event and the second column is a number representing the type of event that occurred. I would like to put certain events and the times they occurred into a different matrix. Specifically, I want events labeled "4" but only if they are preceded by events labeled "16". Any help is appreciated.
0 个评论
采纳的回答
OCDER
2018-8-20
A = [rand(10, 1) [16 4 15 6 16 4 4 16 4 3]'];
Idx = find(A(1:end-1, 2) == 16 & A(2:end, 2) == 4) + 1;
B = A(Idx, :)
A =
0.3299 16.0000
0.9418 4.0000
0.0822 15.0000
0.5926 6.0000
0.2277 16.0000
0.9691 4.0000
0.5915 4.0000
0.5162 16.0000
0.9827 4.0000
0.2968 3.0000
B =
0.9418 4.0000
0.9691 4.0000
0.9827 4.0000
0 个评论
更多回答(1 个)
dpb
2018-8-20
This can probably somehow be done w/ the events objects in the timeseries; the overview doc talks about finding patterns but I've yet to figure out just how it is really helpful for such and can't find any examples in the doc to match the description...
So, way I'd do this would be something like
ev=[e [0;diff(e)]]; % build event lookup table
event=[16 4]; % the event vector to match
ix=find(ismember(ev,[event(2) diff(event)]]); % index to matching event in original array
using e for the 2nd column of data in the matrix.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!