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

4 vues (au cours des 30 derniers jours)
SRB
SRB le 20 Août 2018
Réponse apportée : dpb le 20 Août 2018
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.

Réponse acceptée

OCDER
OCDER le 20 Août 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

Plus de réponses (1)

dpb
dpb le 20 Août 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.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by