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.

채택된 답변

OCDER
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

추가 답변 (1개)

dpb
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by