How to reference a cell in a matrix only if it is preceded by a specific number?

1 view (last 30 days)
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.

Accepted Answer

OCDER
OCDER on 20 Aug 2018
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

More Answers (1)

dpb
dpb on 20 Aug 2018
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.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!