Mulitpling a scalar to specfic numbers in a vector

1 回表示 (過去 30 日間)
jason
jason 2022 年 9 月 7 日
コメント済み: Matt J 2022 年 9 月 8 日
I need to multiply random numbers in a vector by a different scalar depending on what numbers are in the vector. For example, [1 3 5 10 15 17 20 25 30]; I would have to multiply the 1,3 and 5 by 2, and the 10,15,17 by 5, but the 20,25, and 30 would be multiplied by 10. So the vector would look like [2 6 10 50 75 85 200 250 300].

採用された回答

Matt J
Matt J 2022 年 9 月 7 日
編集済み: Matt J 2022 年 9 月 7 日
One way:
[1 3 5 10 15 17 20 25 30].*repelem([2,5,10],3)
ans = 1×9
2 6 10 50 75 85 200 250 300
  3 件のコメント
Torsten
Torsten 2022 年 9 月 7 日
編集済み: Torsten 2022 年 9 月 7 日
Then explicitly form the vector of scalars associated with the elements of the random vector.
[1 3 5 10 15 17 20 25 30].*[2 2 2 5 5 5 10 10 10]
ans = 1×9
2 6 10 50 75 85 200 250 300
Matt J
Matt J 2022 年 9 月 8 日
what would you do if it wasn't in sets of 3
Use this syntax instead,

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by