Seperate One Matrix into two!!

1 view (last 30 days)
Sarit  Hati
Sarit Hati on 8 Nov 2015
Edited: Sarit Hati on 8 Nov 2015
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??!

Accepted Answer

Stephen23
Stephen23 on 8 Nov 2015
Edited: Stephen23 on 8 Nov 2015
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
  1 Comment
Sarit  Hati
Sarit Hati on 8 Nov 2015
Edited: Sarit Hati on 8 Nov 2015
Thank you man!! Thanks a Lot!!
Any way i solved it differently!!(because i'm a noob)
Say tmp3=10 and tm4=5
randsel = randperm (tmp3,tmp4)
randsel2 = [1:1:tmp3];
for n=1:tmp4
for n1=1:tmp3
if randsel(n)==randsel2(n1);
randsel2(n1)=0;
end
end
end
But thanks anyway!!
Cheers!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!