randomly pick number from a matrix in matlab

17,663

Solution 1

If your matrix is M then to pick a random element with uniform probability you can use randi:

 M(randi(numel(M)))

Solution 2

Yes, using randi:

A(randi(numel(A)))
Share:
17,663
happyme
Author by

happyme

Updated on June 04, 2022

Comments

  • happyme
    happyme almost 2 years

    How can i randomly pick a number from the given following matrix below?

        A=[0.06 0.47 0.47]
    

    I just want to randomly pick a number from the matrix above. I am doing this in matlab enviornment. please help. Also, Is it possible assume a variable in matlab that tends to zero, like we do in limits?

  • bla
    bla about 11 years
    length can be not suitable if the array's dimensions are bigger than 1.
  • shoelzer
    shoelzer about 11 years
    True, but the example is 1 x 3 so this does what is asked. I guess that answering the question in general is probably better than just working with the example, though.