Creating multiple matrices with linspace

17 ビュー (過去 30 日間)
Mike Jane
Mike Jane 2020 年 10 月 17 日
コメント済み: Ameer Hamza 2020 年 10 月 18 日
Hello all,
I'm trying to create multiple matrices as follows possibly using linspace:
x = [111;111;111]
x = [222;222;222]
x = [333;333;333]
and so on.
I would then use these matrices as a constant in matrix multiplication in an equation.
The reason would be so I don't have to create 100 matrices manually.
Would this be possible without a loop?
Thanks in advance!
  2 件のコメント
Stephen23
Stephen23 2020 年 10 月 17 日
編集済み: Stephen23 2020 年 10 月 17 日
"Would this be possible without a loop?"
One simple and efficient MATLAB approach:
M = [111;111;111].*(1:9)
And then you can trivially access the columns using basic indexing. Hopefully you are not intending to create 100 separate variables in the workspace, which would be about the worst approach for this task: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Mike Jane
Mike Jane 2020 年 10 月 17 日
Thank you for the reference material I will definitely look into this since I was already creating quite a few variables making the code slow. Much appreciated!

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 17 日
Why do you need linspace for this? You can just create such matrices like this
a = [111; 111; 111];
for i = 1:9
x = a*i;
disp(x)
end
  2 件のコメント
Mike Jane
Mike Jane 2020 年 10 月 17 日
編集済み: Mike Jane 2020 年 10 月 17 日
Ok awesome! This did the trick. Thank you!
Ameer Hamza
Ameer Hamza 2020 年 10 月 18 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by