Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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 ?

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 5 Mär. 2016
Bearbeitet: Roger Stafford am 5 Mär. 2016
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 Kommentare
Susan Arnold
Susan Arnold am 5 Mär. 2016
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 am 5 Mär. 2016
Bearbeitet: Roger Stafford am 5 Mär. 2016
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by