how can i create and plot a vector with elements that are randomly distributed within two regions ?

Hi,
I need to create 1000 data points in each region (as seen in figure below), both regions have identical parameters radius = 10 width = 6.
If anyone knows how I can i do this I would be extremely grateful.
Thanks for your help, Valeria

Answers (1)

clc; clear all ;
C = [0 0] ;
R1 = 10 ;
R2 = R1+6 ;
th = linspace(0,2*pi,500) ;
x1 = C(1)+R1*cos(th) ;
y1 = C(2)+R1*sin(th) ;
%
x2 = C(1)+R2*cos(th) ;
y2 = C(2)+R2*sin(th) ;
%
a = -R2 ; b = R2 ;
xr = (b-a)*rand(1000,1)+a ;
yr = (b-a)*rand(1000,1)+a ;
% get points in the region
idx = inpolygon(xr,yr,[x1 fliplr(x2)],[y1 fliplr(y2)]) ;
plot(x1,y1,'r',x2,y2','r')
hold on
plot(xr(idx),yr(idx),'+k')

This question is closed.

Products

Release

R2020a

Asked:

on 3 Nov 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!