Removing elements and putting them in a different array
Show older comments
Hi everyone, My question is, how can I transform this k=[x1 x2 x3 x4 x5] into this a=[x2 x3] (random values from k) y b=[x1 x4 x5] (the rest), if sum(a)<35. In this example, ive managed to obtain matriz 'a':
clc
clear all
k=[19 16 11 15 8 8 7 14 6 11];
a=k(randperm(10));
i=11;
while 1
i=i-1;
y=randi(i);
if sum(a)<=35
break
else
a(:,y)=[];
end
end
However, when i want to get matriz 'b', it says 'Index exceeds matrix dimensions.'
clc
clear all
k=[19 16 11 15 8 8 7 14 6 11];
a=k(randperm(10));
i=11;
b=[];
while 1
i=i-1;
y=randi(i);
if sum(a)<=35
break
else
a(:,y)=[];
b=[b a(:,y)];
end
end
Thank you in advance for any advice you can offer.
2 Comments
Adam
on 6 Feb 2018
doc setdiff
will give you b without the need of loops.
gonzalo salazar
on 6 Feb 2018
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!