3
$\begingroup$

I am trying to simply do the following

cVec = {a,b,c,d}

From another expression, I obtain another vector, say,

uVec = {k,l,m,n}

What I try to do is to assign each value to other value, like:

a = k
b = l
c = m
d = n

As you may imagine, I do not have four values but several values, so writing each assignment would be cumbersome. I am trying to find easy way to assign the values.

Any help would be really appreciated, thank you.

$\endgroup$
  • 1
    $\begingroup$ Will you do this assignment more than once? Why not consider using an Association instead? Note that both of the answers below will only work once. $\endgroup$ – Carl Woll Oct 21 at 20:23
  • $\begingroup$ I will do this only once, yes. So far, the given answers are sufficient to solve my problem. Since I am quite new to Mathematica, I do not know how Association works. I will check it also. Thank you. $\endgroup$ – nicomedian Oct 21 at 20:24
  • $\begingroup$ @nicomedian Welcome to Mathematica.SE. I have marked your question as a duplicate; please see the links inserted above your post for existing Q&A's on this topic. This does not mean your question is bad, it is a matter of site organization. $\endgroup$ – Mr.Wizard Oct 21 at 22:15
3
$\begingroup$
cVec = {a, b, c, d};
uVec = {k, l, m, n};

With[{z = cVec}, z = uVec];

a

k

also

Clear[a, b, c, d]

x = cVec;

With[{z = x}, z = uVec];
$\endgroup$
  • $\begingroup$ That worked great, thank you. Is there a way not to write explicity cVec in With[] command but define it above? $\endgroup$ – nicomedian Oct 21 at 20:22
  • $\begingroup$ Yes, now updated. $\endgroup$ – Chris Degnen Oct 21 at 20:32
  • $\begingroup$ Thank you for your answer. $\endgroup$ – nicomedian Oct 21 at 20:34
2
$\begingroup$
ClearAll[cVec, uVec, a, b, c, d, k, l, m, n];
cVec = {a, b, c, d};
uVec = {k, l, m, n};
Set @@@ Thread[{cVec, uVec}]
$\endgroup$
  • $\begingroup$ Thank you for your answer. $\endgroup$ – nicomedian Oct 21 at 22:18
1
$\begingroup$

You can use Set wrapping cVec with Evaluate:

Evaluate[cVec] = uVec;
cVec

{k, l, m, n}

c

m

You can also use MapThread:

ClearAll[cVec, a, b, c, d]
cVec = {a, b, c, d};

MapThread[Set, {cVec, uVec}];

cVec

{k, l, m, n}

$\endgroup$
  • $\begingroup$ That also worked great, thank you. $\endgroup$ – nicomedian Oct 21 at 20:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.