2

I have the following code, but Mathematica insists that xj is not a symbol! How may I fix that?

n={5,10,20};
Subscript[x, 1]=Table[0,{Subscript[n, [[1]]]+1}];
Subscript[x, 2]=Table[0,{Subscript[n, [[2]]]+1}];
Subscript[x, 3]=Table[0,{Subscript[n, [[3]]]+1}];
For[j=1,j<=3,j++,
    For[i=0,i<=Subscript[n, [[j]]],i++,
        Subscript[x, j][[i+1]]=i/Subscript[n, [[j]]]; 
   ];
];

and the error is

Set::setps: Subscript[x, j] in the part assignment is not a symbol. >>
Set::setps: Subscript[x, j] in the part assignment is not a symbol. >>
Set::setps: Subscript[x, j] in the part assignment is not a symbol. >>
General::stop: Further output of Set::setps will be suppressed during this calculation. >>

I want to do this,

We have three vectors such as $x_1$,$x_2$ and $x_3$ with length $n_1$, $n_2$ and $n_3$ respectively. Let $n=\{n_1, n_2, n_3\}$. I want to calculate $x_i = \frac{i}{n_j}$, $i=0,\ldots,n_j$ which $j=1,2,3$. I want to express it mathematically as possible as it can be!

NoMan
  • 121
  • 9
  • Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Jan 31 '15 at 17:36
  • 1
    Please revise your question so that the code can be copied from it into Mathematica. – bbgodfrey Jan 31 '15 at 17:37
  • @bbgodfrey Thank you, I have included the main file, please check. – NoMan Jan 31 '15 at 17:50
  • Please add the code to the question. A link is inconvenient. A simple remedy is not to use subscripts (fancy typesetting is sometimes better avoided). – Yves Klett Jan 31 '15 at 18:32
  • 1
    please no attachments. They can contain viruses. No one know what they are downloading here. Also, The code should be pasted here in text, so the question will be valid after the attachment is gone. – Nasser Jan 31 '15 at 18:36
  • Related: Point 2 of this answer. – Michael E2 Feb 01 '15 at 03:49

1 Answers1

2

As explained in Operators without Built-in Meanings, Subscript[x,y] is an operator, not a symbol.

Subscript[x, 1] = Table[0, {i, 3}];
?Subscript[x,1]

Information::nomatch: No symbol matching Subscript[x,1] found. >>

Thus, you cannot apply Part to it, because Part applies only to symbols. This is not associate with your nested For loops and can be obtained simply from

Subscript[x, 1][[2]] = 2

Set::setps: Subscript[x, 1] in the part assignment is not a symbol. >>

An admittedly inelegant alternative is

For[j = 1, j <= 3, j++, 
For[i = 0, i <= n[[j]], i++, y[[j, i + 1]] = i/n[[j]] ]];
y
bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • Actually I don't get it! What am I supposed to do? – NoMan Jan 31 '15 at 18:26
  • I just appended an ugly but functional alternative to my Answer. There may be better alternatives. – bbgodfrey Jan 31 '15 at 18:45
  • @NoMan Don't use Subscripts[] – Dr. belisarius Jan 31 '15 at 18:50
  • It's also not optimal! anyways, Thanks. – NoMan Jan 31 '15 at 18:50
  • @belisarius What is the better alternative, and what do I have to do? – NoMan Jan 31 '15 at 18:51
  • @NoMan The problem to answer that is that your code does nothing, and I can't foresee your intentions. So, how to answer what you should do if I don't know what you want to do? – Dr. belisarius Jan 31 '15 at 18:57
  • 1
    @NoMan Here is one way. n = {5,10,20}; x = {Table[0,{n[[1]]+1}], Table[0,{n[[2]]+1}], Table[0,{n[[3]]+1}]}; For[j=1,j<=3,j++, For[i=0,i<=n[[j]],i++, x[[j,i+1]] = i/n[[j]] ]; ]; and here is a MUCH simpler way n = {5,10,20}; x = Map[Range[0,#]/# &, n] Scrape, paste and test that carefully – Bill Jan 31 '15 at 19:09
  • @Bill Thank you very much, you solved my problem! – NoMan Jan 31 '15 at 19:16
  • @NoMan For loops, like Do loops, print no output. To see the results, print y. Also, I revised my Answer to eliminate all subscripts. – bbgodfrey Jan 31 '15 at 19:24