Search code examples
matlabdatagriddistributionsamplingrandom

Random sampling from gridded data: How to implement this in Matlab?


I have a 200x200 gridded data points. I want to randomly pick 15 grid points from that grid and replace the values in those grids with values selected from a known distribution shown below. All 15 grid points are assigned random values from the given distribution.

The given distribution is:

Given Distribution
314.52
1232.8
559.93
1541.4
264.2
1170.5
500.97
551.83
842.16
357.3
751.34
583.64
782.54
537.28
210.58
805.27
402.29
872.77
507.83
1595.1

The given distribution is made up from 20 values, which are part of those gridded data points. These 20 grid points are fixed i.e. they must not be part of randomly picking 15 points. The coordinates of these 20 points, which are fixed and should not be part of random picking, are:

x   27  180 154 183 124 146 16  184 138 122 192 39  194 129 115 33  47  65  1   93
y   182 81  52  24  168 11  90  153 133 79  183 25  63  107 161 14  65  2   124 79

Can someone help with how to implement this problem in Matlab?


Solution

  • Building off of my answer to your simpler question, here is a solution for how you can choose 15 random integer points (i.e. subscripted indices into your 200-by-200 matrix) and assign random values drawn from your set of values given above:

    mat = [...];   %# Your 200-by-200 matrix
    x = [...];     %# Your 20 x coordinates given above
    y = [...];     %# Your 20 y coordinates given above
    data = [...];  %# Your 20 data values given above
    fixedPoints = [x(:) y(:)];         %# Your 20 points in one 20-by-2 matrix
    randomPoints = randi(200,[15 2]);  %# A 15-by-2 matrix of random integers
    isRepeated = ismember(randomPoints,fixedPoints,'rows');  %# Find repeated sets of
                                                             %#   coordinates
    while any(isRepeated)
      randomPoints(isRepeated,:) = randi(200,[sum(isRepeated) 2]);  %# Create new
                                                                    %#   coordinates
      isRepeated(isRepeated) = ismember(randomPoints(isRepeated,:),...
                                        fixedPoints,'rows');  %# Check the new
                                                              %#   coordinates
    end
    newValueIndex = randi(20,[1 15]);  %# Select 15 random indices into data
    linearIndex = sub2ind([200 200],randomPoints(:,1),...
                          randomPoints(:,2));  %# Get a linear index into mat
    mat(linearIndex) = data(newValueIndex);    %# Update the 15 points
    

    In the above code I'm assuming that the x coordinates correspond to row indices and the y coordinates correspond to column indices into mat. If it's actually the other way around, swap the second and third inputs to the function SUB2IND.