MATLAB: Get a set of 10000 unique random 2-D coordinates

datasamplerandom

I have an array that is 512 rows by 256 columns (i.e Nrows=512, Ncols=256) and I want the 2-d coordinates of a random sample of size 10000 without replacement from this array. I managed to do this with the code below but is seems rather clunky. Seems like there is a cleaner way to do this. Any help appreciated.
Nrows=512; Ncols=256;
row_mat = zeros(Nrows,Ncols);
col_mat = zeros(Nrows,Ncols);
for i=1:Nrows
row_mat(i,:) = i;
end
for i=1:Ncols
col_mat(:,i) = i;
end
ind = [row_mat(:) col_mat(:)];
[temp,idx] = datasample(ind(:,1),10000,'Replace',false);
I = [ind(idx,1) ind(idx,2)]; % This is my list of 10000 unique random coordinates

Best Answer

See if this does what you want:
Array = rand(256,512); % Original Array (Created)
idx = randperm(numel(Array), 1E+4); % Generate Vector OF Random Indices
[row_sub,col_sub] = ind2sub(size(Array), idx); % Convert To Subscripts (If Necessary)
sub_mtx = [row_sub(:), col_sub(:)]; % Matrix Of Subscripts (Rows Of Matrix)