How to generate random coordinates and store the values?
39 ビュー (過去 30 日間)
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.
I want to have the coordinated between -10 to +10. How can I modify the for loop?
Steven Lord 2022 年 1 月 13 日
The easiest solution is not to use a for loop at all.
p = randi([-10 10], 5, 2)
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.