Reputation: 4333
Okay, I use the following code to shuffle the rows of a MxN
matrix called data
:
newData = data(randperm(size(data, 1)), :);
So far so good, what I need now is to keep the first K
rows. Of course I can do this:
newData = data(randperm(size(data, 1)), :);
newData = newData(1:K, :);
But I am trying to do this (just out of curiosity) in just one line. What I tried is this:
newData = data(randperm(size(data, 1)), :)(1:K, :);
Well, it obviously failed. I know it's not important and probably a stupid question but does anyone know a way to do this in one line?
Upvotes: 0
Views: 29
Reputation: 3914
You're randomly permuting your matrix, then taking the top K
rows. Instead, just select K
random rows:
newData = data(randperm(size(data, 1), K), :);
The second argument to randperm
says to choose K
values from 1:size(data, 1)
.
Upvotes: 1