2

How can one create a random matrix (say B) which element B(i,j) is a uniform random integer number in the close interval 0 and A(i,j). A is a given matrix. For example:

A = [6, 2, 5, 7;
     12, 0, 4, 0;
     8, 0, 10, 1;
     11, 6, 2, 5];

1 Answer 1

3
B = floor(rand(size(A)).*(A+1))

Explanation

  • rand(size(A)) creates a matrix of the same size as A, with elements uniformly distributed between 0 and 1.
  • rand(size(A)).*(A+1) each element (i, j) is a random distribution between 0 and A(i, j)+1
  • floor(rand(size(A)).*(A+1)) each unit width range is mapped to the nearest lower integer

Note that B = round(rand(size(A)).*A) will almost do the same, except that 0 and A(i, j) will have less probability to occur.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.