ある条件での配列の作成

11 views (last 30 days)
雅晶
雅晶 on 6 Dec 2022
Commented: 雅晶 on 6 Dec 2022
上の図中の「x」を「new_x」のようにしたいです。
図中の「y」の配列における隣同士の要素の差の絶対値分「x」の要素を増やしたいです。
例えば上記だと、abs(y(1,1) - y(1,2))が3であるので、「x」の配列の最初の要素である214を「new_x」のように3つ連続にするということをしたいです。
どのようにスクリプトを書けばよろしいでしょうか。教えていただきたいです。

Accepted Answer

Hernia Baby
Hernia Baby on 6 Dec 2022
まずはデータを用意します。
y = [168,165,161,159];
x = [214,215,216];
絶対差分であるので abs(y(2) - y(1)) = abs(y(1) - y(2))になります。
なのでfliplrはつかいません。
dy = abs(diff(y))
dy = 1×3
3 4 2
繰り返しはrepelemを使います。
new_x = repelem(x,dy)
new_x = 1×9
214 214 214 215 215 215 215 216 216
  1 Comment
雅晶
雅晶 on 6 Dec 2022
解決しました。ありがとうございました。

Sign in to comment.

More Answers (0)

Categories

Find more on ループと条件付きステートメント in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!