Choose randomly rows in a matrix
1 次查看(过去 30 天)
显示 更早的评论
Hi I have a matrix with 2 columns, the first is for 'ID', the second is for 'age'. I want to randomly pick 'n'(e.g. 2) IDs of the matrix and return the IDs and the age of the same row.
Thanks
0 个评论
回答(1 个)
Sean de Wolski
2013-6-12
doc randperm
Then use the output of this as an index into your matrix.
x = magic(10);
idx = randperm(10,2)
x(idx,:)
3 个评论
Walter Roberson
2013-6-13
You are using an old version of MATLAB that does not support that syntax for randperm. Instead use
idx = randperm(size(x,1));
x(idx(1:2),:)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!