How to create multiple matrices using for loop?

How to take input of coordinates of n number of points and store it in different 2x1 matrices?
The loops I am using are as follows but there is some error.
for i=1:n
x(i)=input('Enter the X-coordinate of node ')
y(i)=input('Enter the Y-coordinate of node ')
end
C=[x;y]
for i=1:n
P(i)= [x(i);y(i)]
end

8 件のコメント

Atsushi Ueno
Atsushi Ueno 2021 年 4 月 17 日
You should treat P as a 2D matrix. I am not sure if this comment solve your question.
P(:,i) = [x(i);y(i)];
Rohit Raut
Rohit Raut 2021 年 4 月 17 日
Thanks for your answer but it still gives the output as a single P matrix. I want multiple 2x1 matrices P1,P2...n
Atsushi Ueno
Atsushi Ueno 2021 年 4 月 17 日
If you mean "different 2x1 matrices" as "tuple (cell array)", you should change your code as below.
P{i} = [x(i);y(i)];
Atsushi Ueno
Atsushi Ueno 2021 年 4 月 17 日
How about this one?
eval(['P' num2str(i) ' = [x(' num2str(i) ');y(' num2str(i) ')]']);
Jan
Jan 2021 年 4 月 17 日
@Atsushi Ueno: Stay at the cell array. Creating a bunch of variables dynamically with EVAL is a bad programming style. See TUTORIAL: Why and how to avoid EVAL
Atsushi Ueno
Atsushi Ueno 2021 年 4 月 18 日
Thank you. I know it is bad programming style, but Rohit Raut wants multiple 2x1 matrices P1,P2...
Stephen23
Stephen23 2021 年 4 月 18 日
"I know it is bad programming style, but Rohit Raut wants multiple 2x1 matrices P1,P2..."
A cell array already contains those "multiple matrices", which are trivially accessible via indexing.
Jonas
Jonas 2021 年 4 月 18 日
i'm a bit confused what you really want. If u are ok with multiple 2x1 matrices in a cell array you can just use num2cell(C,1) in your case, it divides a 2 x n matrix into n cells containing each 2x1 matrices

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2021 年 4 月 17 日

コメント済み:

2021 年 4 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by