Mulitpling diffrent scalars to diffrent numbers in a matrix

1 回表示 (過去 30 日間)
jason
jason 2022 年 9 月 8 日
編集済み: Torsten 2022 年 9 月 8 日
I am trying to multiply several different scalars to a random vector depending on the size of the number. For example, [1 6 3 17 30 7 10 4 25 27], I don't know if these numbers will be the ones chosen though. Each number would be multiplied by a different scalar depending on the size of the number. Anything under 10 would be multiplied by 5, anything between 11 and 26 would be multiplied by 12, and everything else is multiplied by 17. This would make the above vector [5 60 30 204 510 35 100 20 425 459].

回答 (1 件)

Torsten
Torsten 2022 年 9 月 8 日
編集済み: Torsten 2022 年 9 月 8 日
Check your resulting vector. According to your rules, it is wrong.
v = [1 6 3 17 30 7 10 4 25 27];
s = 17*ones(1,numel(v));
s(v<10) = 5;
s(v>11 & v<26) = 12;
s
s = 1×10
5 5 5 12 17 5 17 5 12 17
vs = v.*s
vs = 1×10
5 30 15 204 510 35 170 20 300 459

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by