How do you generate specific values on random points?
2 views (last 30 days)
Show older comments
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.
states = shaperead('usastatehi.shp');
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 , '.')
4 Comments
Star Strider
on 13 Dec 2017
To use 'shaperead', the following product must be licensed, installed, and enabled:
Mapping Toolbox
Answers (2)
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
states = shaperead('usastatehi.shp');
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 , '.')
0 Comments
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:
states = shaperead('GA_Counties.shp');
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.
states = shaperead('USA_States.shp');
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!
2 Comments
Stephen23
on 31 Jan 2018
@Taufiq Rashid: You should post this as a new question, with a link to this one.
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!