samedi 27 août 2016

How to generate a random binary matrix where only one bit in each row has value 1?

I want to use MATLAB to generate a random binary matrix A (n x m) which satisfies a condition:

Each row contains one position with value 1. Other positions are value 0. The position having 1 value is random position.

I tried this code

   n=5;m=10; 
   %% A = randi([0 1], n,m);
   A=zeros(n,m);
   for i=1:n
       rand_pos=randperm(m);
       pos_one=rand_pos(1); % random possition of 1 value
       A(i,pos_one)=1;
   end

Is it correct?




Aucun commentaire:

Enregistrer un commentaire