I need to generate variables with random coordinates according to the user's input. Let's assume that there's a variable NV (number of vertices). If the user says that NV is equal to 4, the function should generate variables x1,y1,x2,y2,x3,y3,x4,y4. Each of the pair (x1-y1,x2-y2 etc.) should represent a vertice with some coordinate.
Kindly help.
Do not do this. If you find yourself having lots of variables x1, x2, x3
etc, in MATLAB, you have made a wrong turn. Even more so if you have to try and create them automatically. You will only make your life more difficult when you attempt to do anything with the variables you've just created.
The better way is to take NV
as a number (how precisely doesn't matter), and make a vector or matrix of the correct size.
For example, to set the x coordinates to random integers (x between 1 and 100, y between 1 and 50)
x = randi(100,[NV 1]);
y = randi(50, [NV 1]);
You can now have your sets of variables as x(1),y(1)
, and so on. Plot them with:
plot(x,y,'*');