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!