Tuesday 15 June 2010

matlab - Random binary matrix with two non-trivial constraints -


Let me create a random matrix of of columns and N I need rows of those rows and zeros such as:

a) There are exactly k in each line. B) Each row is different from the other (the organizer imposes that if n > nchoosek (K, k) will be nchoosek (K, k) rows).

Assume I (all of nchoosek (K, K) = 27405 ), separate 1 Q vector (with K = 30 <)> N = 10000 / Code>) with the K ( k = 4 ) and K - k zeroes.

This code:

  clear all; Closed N = 10000; K = 30; K = 4; M = Randi ([0 1], N, K);  

This code: plot (amount (m, 2)) conditioned a) not satisfied

  clear all; Close; N = 10000; NN = N; K = 30; K = 4; TempM = zero (NN, K); For IE = 1: NN TTModel = TAMPM (ii, :); Ttmodel (randsample (k, k, false)) = 1; % Satisfies the condition a) tempM (ii, :) = ttmodel; End check = bi2de (tempM); From binary to decimal [tresh1, ind, tresh2] = unique (check);% Vatric drop which appears more than once in the matrix M = temp (ind, :); % And satisfied position B) Plot (Amount (M, 2)) Confirms that the condition is to attract the% effective draw, the wanted draw, the number of possible combinations [sum (sum, m, 2) == k ) N. Nokokak (K, K)]   

satisfies the situation a) and partly condition b). I say partly because until NN & gt; & Gt; N will be less than each of each <\ n> in the last matrix.

Is there a better and faster way to solve the problem cycle and N & gt; & Gt; Need for N?

First of all, n Generate:

  cols = randperm (K, N); Cols = cols (:, 1: k);   

Then generate the match line index:

  line = meshgrid (1: N, 1: k) ';   

And finally create this rare matrix with:

  A = sparse (rows, columns, 1, n, k);   

To get the full form of the matrix, use complete (a) .

example
  of = 10; K = 4; N = 5; Cols = randperm (K, N); Cols = cols (:, 1: k); Line = meshgrid (1: n, 1: k) '; A = sparse (rows, columns, 1, n, k); Full (A)   

The result I got:

  ans = 1 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0   

This calculation should be very fast here of = 30, of = 4, of and N > N = The result for 10000 was obtained in less than 0.01 seconds.

No comments:

Post a Comment