find previous value in column before specific set value and delete all others

조회 수: 2 (최근 30일)
Hello,
i have a vector 31x2 (see attachment). I need the previous values of column1 and value of column1 when value of column2 hit values greater then 120. See example result below
182 0
195 13
197 2
207 10
210 3
219 9
222 3
356 134
367 11
371 4
380 9
383 3
388 5
393 5
396 3
529 133
542 13
545 3
554 9
.... ....
Goal (clamp values just for understanding)
222 (3)
356 (134)
396 (3)
529 (133)
Hope you understand it and can help me out. Thank You!

채택된 답변

Rik
Rik 2021년 5월 4일
You can use find to create a vector of indices.
data=[182 0
195 13
197 2
207 10
210 3
219 9
222 3
356 134
367 11
371 4
380 9
383 3
388 5
393 5
396 3
529 133
542 13
545 3
554 9];
ind=find(data(:,2)>120);
ind=ind+[-1 0];%use implicit expansion (use bsxfun pre-R2016b)
ind=ind.';%transpose to make the indexing work as expected
disp(ind)
7 15 8 16
selected=data(ind,:)
selected = 4×2
222 3 356 134 396 3 529 133
disp(selected)
222 3 356 134 396 3 529 133

추가 답변 (1개)

DGM
DGM 2021년 5월 4일
편집: DGM 2021년 5월 4일
Something like this maybe:
load('example.mat')
idx = find(example(:,2)>120);
idx = sort([idx; idx-1]);
valuesiwant = example(idx,1)
gives
valuesiwant =
222
356
396
529
569
702
739
874
911
1047
Alternatively, you can use logical indexing. This is slightly faster and can probably still be improved:
mask = example(:,2)>120;
mask = mask | [mask(2:end); false];
valuesiwant = example(mask,1)

카테고리

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