This is more of a conceptual problem.
Suppose I have three lists.
x1 = {1, 2, 9, 10};
x2 = {5, 6, 7};
x3 = {9, 10, 13};
Now, I wish to keep only the first two elements of each list. More precisely, I want
x1= {1, 2};
x2 = {5, 6};
x3 = {9, 10};
One easy way to do is,
x1=x1[[{1,2}; x2=x2[[{1,2}]]; x3=x3[[{1,2}]];
So far, it is okay. But it won't work if I try to do the same using the Do[]
loop. For example,
Do[i=i[[{1,2}]],{i,{x1,x2,x3}];
do not assign new values to x1,x2,x3.
I also tried,
(#=#[[{1,2}]])&/@{x1,x2,x3}
But it didn't work.
I don't understand the reason behind this. What will be the correct way to assign variables/lists in a Do[]
loop?
#[[{1, 2}]] & /@ {x1, x2, x3}
$\endgroup$f[x_List]:=x[[{1,2}]];
then usex1 = f[x1]
$\endgroup$