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!