Seperate One Matrix into two!!

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??!

 采纳的回答

Stephen23
Stephen23 2015-11-8
编辑:Stephen23 2015-11-8
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 个评论

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!

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by