Question

How to pick a random row from a matrix in Matlab, and also row number of picked row? I'v a matrix named M of 4x3 is following:

  -21.8318   19.2251  -16.0000
   -6.2788    8.6988  -10.0000
    1.5553   -0.8041   -2.0000
   17.6843  -13.0848    0.3000

My question is simple to pick a random row from matrix M (which is ofcourse, not a big deal) with its row number?

Was it helpful?

Solution

Use randi to generate a random integer between 1 and the number of rows of M, and then use it to index into M:

ind = randi(size(M,1)); %// row number
row = M(ind,:); %// corresponding row
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top