MATLAB: Randomly selecting and deleting rows from a matrix

MATLAB

b = X; %X is a 600 by 40 matrix
xnew = b(randperm(427),:);
i want to determine the 173 rows in b that were not selected i could also delete the selected 427 rows from b to obtain the remaining unselected rows can anyone help me do this?
your help is much appreciated

Best Answer

Your code is likely not doing what you think it is doing. The randperm(427) call gives you a permutation of the numbers 1,2,...,427. So ALL of the rows from 1-427 are selected and the rows not selected are always 428-600. From your description, it sounds like what you really wanted was to randomly select 427 out of 600. I.e., is this what you really want?
k = randperm(600,427); % <-- 427 random integers from the set 1-600 (no replacement)
xnew = b(k,:); % <-- select the random rows
m = ~ismember(1:600,k); % <-- get logical indexes of other rows
xother = b(m,:); % <-- the other rows
Another way to get the other rows per your suggestion:
xother = b;
xother(k,:) = [];