2
$\begingroup$

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!

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Jan 31, 2015 at 17:36
  • 1
    $\begingroup$ Please revise your question so that the code can be copied from it into Mathematica. $\endgroup$
    – bbgodfrey
    Jan 31, 2015 at 17:37
  • $\begingroup$ @bbgodfrey Thank you, I have included the main file, please check. $\endgroup$
    – NoMan
    Jan 31, 2015 at 17:50
  • $\begingroup$ 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). $\endgroup$
    – Yves Klett
    Jan 31, 2015 at 18:32
  • 1
    $\begingroup$ 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. $\endgroup$
    – Nasser
    Jan 31, 2015 at 18:36

1 Answer 1

2
$\begingroup$

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
$\endgroup$
9
  • $\begingroup$ Actually I don't get it! What am I supposed to do? $\endgroup$
    – NoMan
    Jan 31, 2015 at 18:26
  • $\begingroup$ I just appended an ugly but functional alternative to my Answer. There may be better alternatives. $\endgroup$
    – bbgodfrey
    Jan 31, 2015 at 18:45
  • $\begingroup$ @NoMan Don't use Subscripts[] $\endgroup$ Jan 31, 2015 at 18:50
  • $\begingroup$ It's also not optimal! anyways, Thanks. $\endgroup$
    – NoMan
    Jan 31, 2015 at 18:50
  • 1
    $\begingroup$ @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 $\endgroup$
    – Bill
    Jan 31, 2015 at 19:09

Your Answer

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

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