Removing elements and putting them in a different array
Afficher commentaires plus anciens
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 commentaires
Adam
le 6 Fév 2018
doc setdiff
will give you b without the need of loops.
gonzalo salazar
le 6 Fév 2018
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!