# How do you generate specific values on random points?

1 ビュー (過去 30 日間)
Ian Bouchard 2017 年 12 月 12 日
コメント済み: Taufiq Rashid 2018 年 1 月 31 日
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 , '.')
##### 4 件のコメント表示 3 件の古いコメント非表示 3 件の古いコメント
Ian Bouchard 2017 年 12 月 13 日
Oh I apologize for not adding that product. I was not thinking about that at all!

サインインしてコメントする。

### 回答 (2 件)

Chris Perkins 2017 年 12 月 15 日
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 , '.')
##### 0 件のコメント表示 -1 件の古いコメント非表示 -1 件の古いコメント

サインインしてコメントする。

Taufiq Rashid 2018 年 1 月 31 日
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!
##### 2 件のコメント表示 1 件の古いコメント非表示 1 件の古いコメント
Taufiq Rashid 2018 年 1 月 31 日
Ok. I will. Thanks!

サインインしてコメントする。

### カテゴリ

Find more on Map Display 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!

Translated by