Find negative values in array, and make them equal to the adjacent values

35 ビュー (過去 30 日間)
TOK
TOK 2020 年 10 月 23 日
回答済み: Voss 2020 年 10 月 23 日
Hi,
I have a single row array where certain values will become negative. Using this array, I would like to create a new array, but change those negative values to be equal to the ones adjacent to them. What is the best way to do this?
Thanks

回答 (1 件)

Voss
Voss 2020 年 10 月 23 日
Adjacent on which side? And what do you do in case the adjacent ones are also negative?
Here's one way to do it, taking the values of the elements previous to the negative elements regardless of whether those themselves are also negative. (And if the first element is negative it doesn't get changed.)
idx = find(x < 0);
idx(idx == 1) = [];
x(idx) = x(idx-1);
Perhaps you can adapt this to suit your requirements precisely.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by