Add elements of array A to array B and ensure there is no repeated element in B?
Afficher commentaires plus anciens
Hi all,
If I have arrays A and B, and I'd like to add elements of array A into array B, during this process, B is actively checked to ensure there is no repeated elements in B, how can I do it?
Many thanks!
2 commentaires
jonas
le 10 Juil 2018
Can you provide example of A and B with the desired output?
Xiaohan Du
le 10 Juil 2018
Réponses (2)
KSSV
le 10 Juil 2018
A = rand(10,1) ;
B = rand(10,1) ;
%%check for B
[C,ia,ib] = unique(B) ;
if length(C)==length(B)
fprintf('B has no repeated elements\n') ;
R = A+C ;
else
fprintf('B has repeated elements\n') ;
end
1 commentaire
Xiaohan Du
le 10 Juil 2018
Guillaume
le 10 Juil 2018
B = unique([B, A]); %assuming B and A are row vectors.
If you want to preserve the original ordering of elements:
B = unique([B, A], 'stable');
Catégories
En savoir plus sur Whos 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!