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

2 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Rik
Rik il 4 Mag 2021
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

Più risposte (1)

DGM
DGM il 4 Mag 2021
Modificato: DGM il 4 Mag 2021
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)

Community Treasure Hunt

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

Start Hunting!

Translated by