matlabuniform

Uniformly distributed points in 2D


How I can generate uniformly distributed points in two dimensions? I tested this code, but I do not want this because in this code x and y are uniform, but the pairs of (x,y) are not uniform.

X=rand(2,N);
x= X(1,:);
y=X(2,:);
figure;                                     
plot(x,y,'.');                              

Solution

  • Your code does uniformly sample the 2D space. But there is also the unifrnd method in Matlab, that samples n-D space.

    N = 5000;
    rng(320);
    X=rand(2,N);
    x=X(1,:);
    y=X(2,:);
    figure('Position',[125 125 1200 500]);                                     
    subplot(1,2,1)
    plot(x,y,'.');  
    
    rng(320);
    X2 = unifrnd(0,1,2,N);
    x=X(1,:);
    y=X(2,:);
    subplot(1,2,2)
    plot(x,y,'.');  
    

    Comparison of methods