


PURPOSE: select a random number of cells in a raster
-------------------------------------------------------------------
USAGE: R = random_cells(n, rastersize, flag);
where: [n] is the number of cells to select
[rastersize] is the size of the raster to select cells
in the form of [rows cols]
[flag] (optional) array in the form [flag noflag] where 'flag' is
the value that is assigned to randomly chosen cells and noflag is
the value of all other cells. Default is [1 NaN]
-------------------------------------------------------------------------
OUTPUTS:
[R] raster with random cells flagged using the value given via
[flag]. Default is 1.
-------------------------------------------------------------------------
Notes: Selected cells can be direct neighbours, but no cell is sampled twice.
Uses: rand, quantile, numel
Felix Hebeler, Geography Dept., University Zurich, August 2006.

0001 function R = random_cells(n, rastersize, flag) 0002 % PURPOSE: select a random number of cells in a raster 0003 % ------------------------------------------------------------------- 0004 % USAGE: R = random_cells(n, rastersize, flag); 0005 % where: [n] is the number of cells to select 0006 % [rastersize] is the size of the raster to select cells 0007 % in the form of [rows cols] 0008 % [flag] (optional) array in the form [flag noflag] where 'flag' is 0009 % the value that is assigned to randomly chosen cells and noflag is 0010 % the value of all other cells. Default is [1 NaN] 0011 % ------------------------------------------------------------------------- 0012 % OUTPUTS: 0013 % [R] raster with random cells flagged using the value given via 0014 % [flag]. Default is 1. 0015 % ------------------------------------------------------------------------- 0016 % Notes: Selected cells can be direct neighbours, but no cell is sampled twice. 0017 % Uses: rand, quantile, numel 0018 % 0019 % Felix Hebeler, Geography Dept., University Zurich, August 2006. 0020 0021 % if ~exist('mindist','var') 0022 % mindist=0; 0023 % end 0024 if ~exist('flag','var') 0025 flag=[1 NaN]; 0026 end 0027 0028 % method used is creating a random surface of the same size as raster and 0029 % selecting all cells above a treshold determined using quantile and flag 0030 % them in the original raster. 0031 R=rand(rastersize(1),rastersize(2)); 0032 th=quantile(R(:),1-(n/numel(R))); 0033 R(R>th)=flag(1); 0034 R(R<=th)=flag(2);