Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Rik
Rik le 4 Mai 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

Plus de réponses (1)

DGM
DGM le 4 Mai 2021
Modifié(e) : DGM le 4 Mai 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)

Catégories

En savoir plus sur Logical 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