How do I generate multiple sets of arrays automatically?
Show older comments
The code currently generates a random scatter of 30 points. An array of x and y values are saved separately in x1 and y1.
I want to make multiple sets of x and y coordinates, so I can have multiple different arrays. For example if I wanted three different random scatters, the code would generate the arrays x1, y1, x2, y2, x3, y3, or if I wanted ten, then there would be ten x y pairings. And each pairing would generate a random scatter of points.
If anyone would be able to advise me, that would be great! I tried making a second for loop, but I couldn't figure out how to make it work... Here's the code I have written so far:
n = 30; %number of elements
R = 0.3; %maximum radius
x1 = zeros(n,1);
y1 = zeros(n,1);
for i = 1:1:n
x1(i) = unifrnd(-R,R);
y1(i) = unifrnd(-R,R);
end
figure(1)
plot(x1,y1, '.')
%save('optArray','x1','y1')
Thanks in advance for your help!
1 Comment
Don't do this! Dynamically named variables are a slow, buggy, amateur, and obfuscated way to program. Using them means removing lots of inbuilt tools that help you to write code properly (code hinting, mlint messages, etc). There are so many reasons why that creating variable names dynamically is such a bad idea (no matter how much beginners love them):
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!