find previous value in column before specific set value and delete all others
1 次查看(过去 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!
0 个评论
采纳的回答
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)
selected=data(ind,:)
disp(selected)
0 个评论
更多回答(1 个)
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Mobile Fundamentals 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!