Reputation: 11
I wrote this matlab code to plot a set of random points in a specific region of the graph. I need to set the xlim
and ylim
in the range (1,512) (-1,512)
but when I substituted this values in the following code nothing is plotted.I tried also to insert the range 150,350
that represent the central part of the graph in which I want to plot all points. How can I fix the problem?
x = rand(1, 50);
y = rand(1, 50);
plot(x,y,'.')
xlim([-0.2 1.2])
ylim([-0.2 1.2])
Upvotes: 0
Views: 771
Reputation: 65440
The output of rand
is going to contain values between 0
and 1
so when you expand the x and y limits to [1 512]
all data is going to be shown within the lower 1/512th of the axes and therefore you can't see the individual points.
If you want your random values to actually span the range [1 512]
(for x
) and [-1 512]
(for y
), then you'll want to alter the output of rand
accordingly.
x = 1 + rand(1, 50) * 511;
y = rand(1, 50) * 513 - 1;
plot(x, y, '.')
xlim([1 512]);
ylim([-1 512]);
A more general solution would be to create an anonymous function which creates random numbers within the specified range
myrand = @(r, varargin)rand(varargin{:}) * diff(r) + min(r);
xrange = [1 512];
yrange = [-1 512];
x = myrand(xrange, 1, 50);
y = myrand(yrange, 1, 50);
plot(x, y, '.')
xlim(xrange);
ylim(yrange);
Or if you want your points to be within a certain region inside of the axes
x = myrand([50 100], 1, 50);
y = myrand([50 100], 1, 50);
plot(x, y, '.');
xlim([0 150])
ylim([0 150])
Upvotes: 2