lundi 7 mars 2016

Matlab - Only allow fixed number of random points in each tile

Following on from a previous question, I have a code that I think should limit the number of randomly generated points in each quadrant of the total tile, however it is not working.

n = 4;
used = [];
k = 0;
a1_count = 0;
a2_count = 0;
a3_count = 0;
a4_count = 0;
min = 1;
max = 1;

while k<n
    x = rand*2;
    y = rand*2;

    notvalid = 0;

    if (0 <= x) && (x <= 1) && (0 <= y) && (y <= 1)
        a1_count = a1_count + 1;
    end
    if (1 < x) && (x <= 2) && (0 <= y) && (y <= 1)
        a2_count = a2_count + 1;
    end
    if (0 <= x) && (x <= 1) && (1 < y) && (y <= 2)
        a3_count = a3_count + 1;
    end
    if (1 < x) && (x <= 2) && (1 < y) && (y <= 2)
        a4_count = a4_count + 1;
    end
    %%%
    if (min <= a1_count) && (a1_count <= max) && (min <= a2_count) && (a2_count <= max)...
            && (min <= a3_count) && (a3_count <= max) && (min <= a4_count) && (a4_count <= max)
        notvalid=1;
    end

    if notvalid
        continue
    end

    used(end+1,:) = [x;y];
    k = k+1;


end

I wish to generate 4 random points, and have one in each quadrant of the total area. To do this, I have a maximum and minimum number of points in each quadrant (in this case 1), and an if statement to check that the count for each tile falls within the min and max. If it doesn't, then notvalid = 0 and the loop should begin again. This function doesn't seem to work however, as the loop finishes with 4 points total and is completely random (all the counts should = 1).

Can anyone spot where I'm going wrong?




Aucun commentaire:

Enregistrer un commentaire