Create 100 random particles(all the same size) in an empty plot with dimensions (x-axis from -16 to 16) and (y-axis from -16 to 16) and (angle from 0 to 2*pi)

This question was flagged by Paul Hoffrichter
  • Flagged as Duplicate by Paul Hoffrichter on 27 Apr 2021.

    Duplicate questions (as of 2021-04-27): https://www.mathworks.com/matlabcentral/answers/806792-index-in-position-1-is-invalid-array-indices-must-be-positive-integers-or-logical-values?s_tid=prof_contriblnk https://www.mathworks.com/matlabcentral/answers/805766-index-in-position-1-is-invalid-array-indices-must-be-positive-integers-or-logical-values?s_tid=prof_contriblnk https://www.mathworks.com/matlabcentral/answers/813670-create-100-random-particles-all-the-same-size-in-an-empty-plot-with-dimensions-x-axis-from-16-to?s_tid=srchtitle https://www.mathworks.com/matlabcentral/answers/809330-index-in-position-1-exceeds-array-bounds-must-not-exceed-641-error-in-measurements-line-117?s_tid=prof_contriblnk

Create 100 random particles(all the same size) distributed uniformly in an empty plot with dimensions (x-axis from -16 to 16) and (y-axis from -16 to 16) and (angle from 0 to 2*pi). I must avoid the furniture or the obstacles in my map so my map function as below:
xw=-16; xe=16; ys=-16; yn=16; %map dimensions, west, east, south, north
mrx=0.05; mry=0.05; % map resolution {x,y}
[xm ym]=meshgrid(xw:mrx:xe,ys:mry:yn);
m=0*xm.*ym; % initialize the map m
for i=1:size(xm,2)
for j=1:size(ym,1)
r=sqrt(xm(1,i)^2+ym(j,1)^2);
ang=atan2(ym(j,1),xm(1,i))+pi;
if 9<=r && r<=10 && (pi/15<=ang && ang<=29*pi/15)
m(j,i)=1;
elseif 6<=r && r<=7 && ~(pi-pi/6<=ang && ang<=pi+pi/6)
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-9 && -2.8<=ym(j,1) && ym(j,1)<=-1.8
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-9 && 1.8<=ym(j,1) && ym(j,1)<=2.8
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-13 && -2.8<=ym(j,1) && ym(j,1)<=14
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-10 && 2.8<=ym(j,1) && ym(j,1)<=11
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<14 && 14<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<11 && 11<=ym(j,1) && ym(j,1)<=12
m(j,i)=1;
elseif 14<=xm(1,i) && xm(1,i)<15 && -15<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -5<=xm(1,i) && xm(1,i)<14 && -15<=ym(j,1) && ym(j,1)<=-14
m(j,i)=1;
elseif 10<=r && r<=16 && 0.95*pi/3.5<=ang && ang<=1.05*pi/3.5
m(j,i)=1;
elseif xm(1,i)<0.98*xw || 0.98*xe<xm(1,i) || ym(j,1)<0.98*ys || 0.98*yn<ym(j,1) % Boundaries
m(j,i)=1;
end
end
end
mesh(xm,ym,m); axis image; view(0, 90); %xlabel('x'); ylabel('y');

11 Comments

My question is how can I write a matlab code to create 100 random particles(all the same size) distributed uniformly in an empty plot with dimensions (x-axis from -16 to 16) and (y-axis from -16 to 16) and (angle from 0 to 2*pi). I must avoid the furniture or the obstacles in my map and my map function as above.
Ah, ok. I didn't understand that your code only creates the map.
You need to avoid the yellow area, or the purple/blue area?
They are not duplicate questions. I am working on project which has many parts and each question from differnet part. And no one helped me till now.
There are two methods:
  1. generate random points and reject those that are in an invalid position
  2. design a procedure that guarantees you will not get any points that are invalid
Which one did you try?
What issues are you having in implementing that? Do you have issues generating random numbers? Or detecting if the points are invalid?

Answers (0)

This question is closed.

Tags

Asked:

on 26 Apr 2021

Closed:

on 29 Apr 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!