Re: to input zeros randomly into matrix



Wilson wrote:


Hi Guys,

If i have a random matrix such as

matrix =

1 0 1 0 0 1 0 1 0 1
0 1 1 1 0 1 0 1 0 0
0 1 0 0 1 0 0 1 1 1
1 0 0 1 0 0 1 1 0 1
0 1 0 1 1 0 1 1 1 0
1 1 0 1 1 0 1 1 1 1
0 0 1 0 1 0 1 1 0 0
1 0 1 0 1 0 0 0 1 0
1 0 1 0 0 0 0 0 0 1
1 1 0 1 0 1 0 1 0 0

and

sum(matrix)

ans =

6 5 5 5 5 3 4 8 4 5

is there anyway for me to be able to check if say for every column
there should be more than 2 ones and no more than 5 ones? if there
is
more than 5 ones for eg, i have to randomly change 1 = 0 for any
(i,j) component

Cheers!~

To create a M-by-N matrix a with a random number of ones between 2
and 5 in each of the e.g. N columns, you can use:

M = 10 ;
N = 6 ;
n = 1+ceil(4*rand(1,6)) ; % random
X = nones(n) ;
if M > size(X,1), X(M,N) = 0 ; end
X = shake(X) ;
isequal(sum(X),n) % test

Both <shake> and <nones> can be found on the matlab file
exchange.

hth (getting your grade)
Jos
.



Relevant Pages

  • Re: Sorting the column in a matrix
    ... After I produce a random matrix with zeros and ones, ... then put the same column in one empty array, ...
    (comp.soft-sys.matlab)
  • Sorting the column in a matrix
    ... After I produce a random matrix with zeros and ones, ... then put the same column in one empty array, ...
    (comp.soft-sys.matlab)