How to generate a set of variables with increasing numbers related to the indices of another array?

2 ビュー (過去 30 日間)
Given:
x=[3 1 2]
y=[1 1 1]
Is there a way to generate some code to do something like this in MatLab?
x1=y.*x(1)
x2=y.*x(2)
x3=y.*x(3)
where we generate a new variable with a numerical value that correspond to the indices of the array used in the operation?
So the answer should be:
x1=3 3 3
x2=1 1 1
x3=2 2 2
I hope it is clear enough!
Thank you!
  1 件のコメント
madhan ravi
madhan ravi 2019 年 8 月 22 日
Seba.V interesting you show the expected result as one and you acknowledge the result which is completely the other way around.

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

採用された回答

Star Strider
Star Strider 2019 年 8 月 22 日
編集済み: Star Strider 2019 年 8 月 22 日
You can do that more easily with simple matrix-vector multiplication:
z = y(:)*x
producing:
z =
1 1 1
2 2 2
3 3 3
and if you absolutely must create separate vectors for them from ‘z’ (although this is not considered to be efficient programming practice), that is straightforward:
x1 = z(1,:)
x2 = z(2,:)
x3 = z(3,:)
x1 =
1 1 1
x2 =
2 2 2
x3 =
3 3 3

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by