How to generate random coordinates and store the values?

63 ビュー (過去 30 日間)
Ashfaq Ahmed
Ashfaq Ahmed 2022 年 1 月 13 日
コメント済み: Steven Lord 2022 年 1 月 13 日
Hi all,
Suppose, I want to randomly create 5 different coordinates such as p(x,y) = (1,3), (-5,2), (8,1), (-4,4), and (6,3).
And I want to save the values in my workspace as a 5x2 double marrix, like p = [1,3; -5,2; 8,1; -4,4; 6,3];
How can I do that?
I tried to use the randi function, but it's only saving the final coordinates insetad of saving all five.
for i=1:5
p =randi([-10,10],1,2);
p
end
I want to have the coordinated between -10 to +10. How can I modify the for loop?

採用された回答

Steven Lord
Steven Lord 2022 年 1 月 13 日
The easiest solution is not to use a for loop at all.
p = randi([-10 10], 5, 2)
p = 5×2
-5 -7 3 5 2 -1 9 -1 5 -6
If you must use a for loop, preallocate p to be the correct final size using ones, zeros, etc. then fill in the rows using indexing.
  5 件のコメント
Rik
Rik 2022 年 1 月 13 日
Dumb questions are fine to ask. Everybody starts out asking dumb questions. The chance this thread will help someone else with a similar problem (tomorrow, or a few years from now) is actually higher with beginner questions.
So just leave this thread as it is. The one thing you can still do is to mark the answer as accepted answer.
Steven Lord
Steven Lord 2022 年 1 月 13 日
This isn't a dumb question. It's a new user question. One of the main ways users transition from "new user" to "user" is by asking new user questions or reading the answers to new user questions asked by previous new users.

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by