MATLAB: Seperate One Matrix into two!!

MATLABmatrixmatrix manipulation

Say, I have a matrix
a = [1 2 3 4 5 6 7 8 9 10]
randsel = randperm (10,5)
Say randsel returns [1 3 5 6 10]
Now, I want to select the rest of the numbers and put it into another matrix ,say 'randsel2'
How to do that??!

Best Answer

If you generate all of the indices using randperm, then you can simply select the first half of those indices and the second half: these are all random indices, so the two selections are also random.
>> a = 20:29 % twenties to distinguish from the indices.
a =
20 21 22 23 24 25 26 27 28 29
>> idx = randperm(10); % generate all indices
>> a(idx(1:5)) % first half of random indices to select one set
ans =
29 28 25 21 20
>> a(idx(6:end)) % second half of random indices to select reamining
ans =
22 27 26 23 24