0
$\begingroup$

Now, I want to assign some values for a Table list and I find that

If

 {mat[1], mat[2], mat[3], mat[4], mat[5]}={4,23,3,12,1};

it is no problem. But, if we use

  Table[mat[n],{n,1,5}]={4,23,3,12,1};

It doesn't work. Why ? Thanks.

$\endgroup$
2

2 Answers 2

1
$\begingroup$

You should evaluate the left-hand side first.

Evaluate[Table[mat[n], {n, 5}]] = {4, 23, 3, 12, 1}

Another possible way:

With[{values = {4, 23, 3, 12, 1}},
 Do[mat[n] = values[[n]], {n, 5}]]
$\endgroup$
1
  • $\begingroup$ The first method will not work the second time. The second will, however. +1 $\endgroup$
    – Mr.Wizard
    Feb 3, 2015 at 9:19
0
$\begingroup$

You must tell the kernel to evaluate the left hand side before invoking Set Try this:

Evaluate[Table[mat[n],{n,1,5}]]={4,23,3,12,1};
$\endgroup$
2
  • $\begingroup$ Be aware that this will not work the second time. $\endgroup$
    – Mr.Wizard
    Feb 3, 2015 at 9:19
  • $\begingroup$ Well then, do it right the first time around :-) $\endgroup$
    – Jason B.
    Feb 3, 2015 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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