vendredi 1 avril 2022

Random matrix of Integers with fixed row sums in Julia

I would like to create a random matrix with the following constraints:

  • all values should be Integer values
  • fixed row sums (decreasing with row e.g. 100, 50, 10, 5 etc.)
  • random values from 0 to 6

I tried to create a random matrix and scale it to my fixed row sums I want, but then I get the problem that I get Float values (rounding does not help either)

A = rand(0:6, 5, 10)

sum_vec = [10,5,3,2,2]

for i = 1:length(A[:,1])
    # row sum 
    s = sum(A[i,:])
    A[i,:]  = (A[i,:] /s ) * sum_vec[i]
end

Thanks a lot for your help!




Aucun commentaire:

Enregistrer un commentaire