matlab - How to generate random matrix without repetition in rows and cols? -
matlab - How to generate random matrix without repetition in rows and cols? -
how generate random matrix without repetition in rows , cols specific range
example (3x3): range 1 3
2 1 3 3 2 1 1 3 2
example (4x4): range 1 4
4 1 3 2 1 3 2 4 3 2 4 1 2 4 1 3
this algorithm trick, assuming want contain elements between 1
, n
%// elements contained, no 0 allowed = [1 2 3 4]; %// possible permutations , size n = numel(a); %// initialization output = zeros(1,n); ii = 1; while ii <= n; %// random permuation of input vector b = a(randperm(n)); %// concatenate found values temp = [output; b]; %// check if row chosen in iteration exists if ~any( arrayfun(@(x) numel(unique(temp(:,x))) < ii+1, 1:n) ) %// if not, append output = temp; %// increment counter ii = ii+1; end end output = output(2:end,:) %// delete first row zeros
it won't fastest implementation. curios see others. computation time increases exponentially. 7x7 bearable.
matlab random matrix
Comments
Post a Comment