function R = rtsRandomizeMask(mask, N) %error checking if N > nnz(mask) N = nnz(mask); end if N < 0 N = 0; end %get the indices of all nonzero values in the mask ind = find(mask); %randomize the indices rind = ind(randperm(size(ind, 1))); %create the new randomized mask (random subset of the old mask) R = zeros(size(mask)); R(rind(1:N)) = 1; R = R > 0;