1

Is there anyone who know any function (or any suggestion) in MATLAB, that I could spread nodes on a 2D domain?

It is necessary for me that spread nodes so that the nodes density be nearly equal on whole domain.

I am grateful to you for your help.

Rosa
  • 253
  • 1
  • 14

1 Answers1

4

How about rand?

If you need 2D coordinates of n points spread uniformly on a 2D domain, with width w and height h then:

xy = bsxfun(@times, rand( 2, n ), [w; h] );
figure; scatter( xy(1,:), xy(2,:), 40, '+' );axis equal;

Here's a possible result for n=500, h=3 and w=1: enter image description here

Shai
  • 111,146
  • 38
  • 238
  • 371
  • thanks a lot,but how could I use it for any 2D domain, for example circle? should I change [w;h]? – Rosa Nov 20 '13 at 13:05
  • for arbitrary rectangular domains you can rotate `xy` using 2D transformation. However, using arbitrary transformations that are beynod rotation, translation, scaling the resulting density might not be uniform anymore. – Shai Nov 20 '13 at 13:11
  • 1
    For a triangular spread see [this question](http://stackoverflow.com/questions/14021381/matlab-generate-and-plot-a-point-cloud-distributed-within-a-triangle). Note how people addressed the subject of **uniform** spread in their answers there! – Shai Nov 20 '13 at 13:13
  • For a circular shape, the easiest method surely is a rejection method: generate `xy = -R + 2*R*rand(n,2);` and reject all points that are outside the circle: `xy(sum(xy.^2,2) > R^2, :) = [];`. – sebastian Nov 20 '13 at 14:50