필터 지우기
필터 지우기

Create/choose a rondom point from an existed data matrix?

조회 수: 1 (최근 30일)
Susan Arnold
Susan Arnold 2016년 3월 4일
편집: Roger Stafford 2016년 3월 5일
I have data matrix (3000 * 3) and then I created from these data delaunay triangles with vertices P0, P1 and P2. I want to establish rondom points Pr which come originally from the data matrix (3000 * 3) and these random points are located inside the generated delaunay triangles vertices P0, P1 and P2. Could you please help me to achieve this ?

채택된 답변

Roger Stafford
Roger Stafford 2016년 3월 5일
편집: Roger Stafford 2016년 3월 5일
I will give an answer in terms of a single triangle with vertices at P0, P1, and P2, (either in 2D or 3D.) Define
r1 = rand;
r2 = sqrt(rand); % <-- The square root operation is important here
Then the value P
P = (1-r2)*P0+r2*(r1*P1+(1-r1)*P2);
will be a random point within the triangle P0P1P2, with a statistically uniform probability distribution therein.
I will leave the problem of doing this for the entire triangulation to you. It can easily be vectorized.
  댓글 수: 5
Susan Arnold
Susan Arnold 2016년 3월 5일
I tried to apply inpolygon function, but I do not know how to apply it for three vertices. Please I need your help.
Roger Stafford
Roger Stafford 2016년 3월 5일
편집: Roger Stafford 2016년 3월 5일
Removing the square root operation will still generate points within the triangle, but they will not be statistically distributed uniformly area-wise within the triangle. They will tend to concentrate near the P0 vertex, which I assume you do not want.
You can verify this using the following example, both with and without the 'sqrt' operation in m2:
P0 = randn(1,2); % Random triangle
P1 = randn(1,2);
P2 = randn(1,2);
N = 4000; % Number of random points generated within triangle
m1 = rand(N,1);
m2 = sqrt(rand(N,1));
P = (1-m2)*P0+(m2.*m1)*P1+(m2.*(1-m1))*P2;
plot(P(:,1),P(:,2),'y.',... % Yellow dots within red triangle
[P0(1),P1(1),P2(1),P0(1)],[P0(2),P1(2),P2(2),P0(2)],'r-')
axis equal
For values of 0<=m1<=1 and 0<=m2<=r, the area covered in the triangle is another smaller similar triangle whose area is proportional to the square of r. This fact is what justifies the use of the square root above in conjunction with the 'rand' function. See the discussion in:
https://en.wikipedia.org/wiki/Triangular_distribution
in the "Generating Triangular-distributed random variates" section, which is closely related to this, or perhaps the 2012 discussion at:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323426

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by