Move and swap elements between vectors using loops and functions?

lets say there are four vectors each has different dimension as follow: A=[5 7 11 15 17], B=[4 14 19 21], C=[2 6 9 10 13 18], D=[3 8 12 16 20]
First, I wan to remove elements of A to B one by one(each time one element should be moved) vice versa from B to A, and the same for C and D. Secondly, I want to swap elements of A with B ( also each time just one swap), and the same for C and D.
How can I do both steps move and swap using loops and functions?

4 commentaires

Jan
Jan le 6 Déc 2017
Modifié(e) : Jan le 6 Déc 2017
This sounds like a homework question. Then please mention, if it is one, because this demands for another kind of answering. Post, what you have tried so far and ask a specific question. It would not be useful, if the forum solves your homework.
What does "remove elements of A to B" mean? What should be removed? A and B do not have common elements.
How can you swap elements of A and B one by one, if they have a different number of elements?
If this is really a homework, let me mention, that this is the most useless problem I've ever seen. I cannot imagine, what you should learn from programming this strange process.
Hi Jan, This is not a homework, in fact it is a real problem, so if you can help please do it, otherwise let's see if somebody can help. I know it is difficult, for this reason I ask, also I know that it is not easy to make a loop or function to move or remove elements and to swap, but I hope someone who is expert can help.
@Hajem: Okay, no homework. Then please answer my questions for clarifications. What does "remove elements from A to B" mean? Create the wanted output manually for the posted example data. The same for "swap elements". The terms are neither unique nor clear.
@jan, what I mean by remove elements one each time is to make a loop or function to do it dynamically, for example, start by the first element of A which is 5 and remove it to B, so B=[5 4 14 19 21], and then the second element of A which is 7 and remove it to B, so B this time is =[7 4 14 19 21] and repeat for all elements of A to B and the same thing from B to A.
For swapping, is to replace the elements of A with B, for example in the first swap, we replace the first element of A with the first element of B so A=[4 7 11 15 17] and B become B=[5 14 19 21] and then the same for all elements of A and B. The same thing should be done for C and D. I hope this clear for you.

Connectez-vous pour commenter.

 Réponse acceptée

Vague question → vague answer:
% swap at position k
k = 3
c = A(k)
A(k) = B(k)
B(k) = c

1 commentaire

Thank you, Jos, but this replaces just one element of each vector. What I need is replace(swap) all elements dynamically using loop or function.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by