# How do you generate specific values on random points?

3 views (last 30 days)
Ian Bouchard on 12 Dec 2017
Commented: Taufiq Rashid on 31 Jan 2018
Hello, I am currently attempting to work on a code that generates values on random points. What I would like to to would have random points generated where the first point is the value of 1, then the random point selected will have a value of 0, then the next random point selected will have a value of 1 where the values of the random points selected alternates between the value of 0 and 1. I currently have random points generated on my code contained withing a specific polygon, but I am having a hard time giving these points the wanted values. I would also like to be able to generated a plot where the values are shown. Any help will be much appreciated! The usastatehi.shp file that I have attached is only the text file. I unfortunately do not know how to attach the proper link, but the file is available in the mapping toolbox.
st = states(47); %creates a polgon in the shape of Washington State
stBB = st.BoundingBox;
st_minlat = min(stBB(:,2 ));
st_maxlat = max(stBB(:,2 ));
st_latspan = st_maxlat - st_minlat;
st_minlong = min(stBB(:,1 ));
st_maxlong = max(stBB(:,1 ));
st_longspan = st_maxlong - st_minlong;
stX = st.X ;
stY = st.Y;
numPointsIn = 7;
for i = 1:numPointsIn
flagIsIn = 0;
while ~flagIsIn
x(i) = st_minlong + rand(1) * st_longspan ;
y(i) = st_minlat + rand(1) * st_latspan ;
flagIsIn = inpolygon(x(i), y(i), stX, stY );
end
end
mapshow(st, 'edgecolor', 'r', 'facecolor', 'none ')
hold on
scatter(x, y , '.')

Show 1 older comment
Ian Bouchard on 12 Dec 2017
I apologize but I forgot to mention that I used the Mapping Toolbox product which I believe is what contained the 'usastatehi.shp'.
Star Strider on 13 Dec 2017
To use 'shaperead', the following product must be licensed, installed, and enabled:
Mapping Toolbox
Ian Bouchard on 13 Dec 2017
Oh I apologize for not adding that product. I was not thinking about that at all!

Chris Perkins on 15 Dec 2017
Hi Ian,
If I understand your question correctly, you want to be able to associate a certain number (either 0 or 1) with each of the points that you are generating using "rand".
You can just create an array of alternating O's and 1's, and then the first element of the 'values' array would correspond to the first elements in 'X' and 'Y'. You can add the values to your graph using the "text" function.
Here is your example, with added code to display either a '1' or a '0' at each point:
% Original Code
st = states(47); %creates a polgon in the shape of Washington State
stBB = st.BoundingBox;
st_minlat = min(stBB(:,2 ));
st_maxlat = max(stBB(:,2 ));
st_latspan = st_maxlat - st_minlat;
st_minlong = min(stBB(:,1 ));
st_maxlong = max(stBB(:,1 ));
st_longspan = st_maxlong - st_minlong;
stX = st.X ;
stY = st.Y;
numPointsIn = 7;
% moved these two lines earlier in the script
mapshow(st, 'edgecolor', 'r', 'facecolor', 'none ')
hold on
for i = 1:numPointsIn
flagIsIn = 0;
while ~flagIsIn
x(i) = st_minlong + rand(1) * st_longspan ;
y(i) = st_minlat + rand(1) * st_latspan ;
flagIsIn = inpolygon(x(i), y(i), stX, stY );
end
% Modified Code
if mod(i,2) == 1
values(i) = 1;
else
values(i) = 0;
end
disp(x(i));
disp(y(i));
disp('');
% Adding 0.1 so the text doesn't cover the point itself
text(x(i)+0.1,y(i),num2str(values(i)));
% End Modified Code
end
scatter(x, y , '.')

Taufiq Rashid on 31 Jan 2018
Hi, I am also working on a similar problem. In my case, I have a shapefile of Georgia and I want to create random points inside the polygon of Georgia. But, no points are generated if I use the following code:
stBB = st.BoundingBox;
st_minlat = min(stBB(:,2 ));
st_maxlat = max(stBB(:,2 ));
st_latspan = st_maxlat - st_minlat;
st_minlong = min(stBB(:,1 ));
st_maxlong = max(stBB(:,1 ));
st_longspan = st_maxlong - st_minlong;
stX = st.X ;
stY = st.Y;
numPointsIn = 10;
for i = 1:numPointsIn
flagIsIn = 0;
while ~flagIsIn
x(i) = st_minlong + rand(1) * st_longspan ;
y(i) = st_minlat + rand(1) * st_latspan ;
flagIsIn = inpolygon(x(i), y(i), stX, stY );
end
end
mapshow(st, 'edgecolor', 'r', 'facecolor', 'none')
hold on
scatter(x, y , '.')
However, if I use the shapefile of US States and then create a polygon for Georgia, the code works.
st = states(45); %creates a polgon in the shape of Georgia State
For my particular problem, I need to work directly with shapefile of Georgia. How can I save the imported shapefile as a polygon and make it work? Any help would be highly appreciated!

Stephen Cobeldick on 31 Jan 2018
@Taufiq Rashid: You should post this as a new question, with a link to this one.
Taufiq Rashid on 31 Jan 2018
Ok. I will. Thanks!