1

I'd like to assign to $a[1],\ldots,a[m]$ ($m$ being defined beforehand) zero values. I tried

Table[a[i], {i, m}] := Table[0, {m}];

but got an error message

Set::write: "Tag Table in Table[Sub[Table[0, {i}]], {i, 2, m}] is Protected. 

Correction: Both suggested methods work well but in my particular situation which differs slightly from the above example the former method gives a strange result

m := 4;
SubY[i_] := Subscript[y, i];
Table[SubY[i], {i, m}]
Table[SubY[i] = 0, {i, m}];
Table[SubY[i], {i, m}]
Subscript[y, 2]

{y_1, y_2, y_3, y_4}
{0, 0, 0, 0}
y_2

while the latter method works perfectly well.

m := 4;
SubG[i_] := Subscript[g, i];
Table[SubG[i], {i, m}]
MapThread[(#1 := #2) &, {Table[SubG[i], {i, m}], Table[0, {m}]}];
Table[SubG[i], {i, m}]
Subscript[g, 2]

{g_1, g_2, g_3, g_4}
{0, 0, 0, 0}
0
8k14
  • 225
  • 1
  • 6
  • 5
    Don't use subscripts until you get a very good grip on the language. You're calling for trouble – Dr. belisarius Jan 16 '16 at 11:33
  • A big problem in your code is that you can't use _ in your variables name. If you want to enter y suscripted by 1, you must type y then ^6 then 1 , or use the menus. – andre314 Jan 16 '16 at 11:38
  • I recommend that you read (6511) and the questions linked from it. I echo belisarius' warning. Subscript is tricky even for experienced users. – Mr.Wizard Jan 16 '16 at 22:35

2 Answers2

1

Will this simple method work for you?

 m = 5;
 Table[a[i] = 0, {i, m}];
Hubble07
  • 3,614
  • 13
  • 23
  • Thanks. It works but actually I need something a bit different. I'm expanding my question. – 8k14 Jan 16 '16 at 11:14
1

If $a$ is already defined

SeedRandom[123];
a = RandomInteger[{1, 9}, 6]
(* {8, 5, 1, 3, 7, 8} *)

and Length[a]>=m then

m = 4;
a[[;; m]] = ConstantArray[0, m];
a
(* {0, 0, 0, 0, 7, 8} *)

However, if $a$ not already defined then create with length $m$ of zeros.

m = 4;
a = ConstantArray[0, m]
(* {0, 0, 0, 0} *)

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143