I feel like I'm missing something obvious here, but....

I'm playing around, trying to do the standard deviation for my stats homework (don't worry, I already worked out the answer by hand), and I have

μ := 0.906
y = {0.343, 0.441, 0.189, 0.027}
\*SubsuperscriptBox[\(∑\), \(i = 0\), \(3\)]\(
\*SuperscriptBox[\((i - μ)\), \(2\)]\ 
\*SubscriptBox[\(y\), \([i]\)]\)\)]

which looks like $ \sqrt{\sum _{i=0}^3 (i-\mu )^2 y_i} $

Now that looks like what I want, but it does not evaluate how I'd hope.

So I tried out $y_1$ on it's own, and just get the whole list followed by subscript 1, rather than the second (or third...) element.

Ok, so that's fine, if a little disappointing that I can't index into a vector as easily as that. But I can't for the life of me figure out how to cleanly achieve the goal of retrieving the ith element of a vector within an expression like this.

  • $\begingroup$ change the last line to Sqrt[\!\( \*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(3\)]\( \*SuperscriptBox[\((i - \[Mu])\), \(2\)]\ \*SubscriptBox[\(y\), \([\([i]\)]\)]\)\)]? $\endgroup$
    – kglr
    Apr 19, 2015 at 22:47
  • $\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
    Apr 19, 2015 at 22:51
  • $\begingroup$ ... (1) in Mathematica indices start at 1, not 0 (2) you need to use [[i]] to refer to ith Part of a list, (3) Part syntax y[[i]] also works in subscripts. (4) Much simpler form Sqrt[Sum[(i - \[Mu])^2 y[[i]], {i, 1, 4}]] gives the same result. $\endgroup$
    – kglr
    Apr 19, 2015 at 22:51
  • $\begingroup$ kguler, thanks for the help. You don't want to put that as an answer? Also, I don't see how the second version would give the same result--: $ 1-\mu \neq 0-\mu $, right? or am I missing something $\endgroup$
    – Ben
    Apr 19, 2015 at 22:56
  • $\begingroup$ @Ben, if i in i-\[Mu] is the same as the part index, maybe you need to change the sum to Sqrt[Sum[(i - 1 - \[Mu])^2 y[[i]], {i, 1, 4}]]? $\endgroup$
    – kglr
    Apr 19, 2015 at 23:17

3 Answers 3


First, you can get the desired result using much simpler form

Sqrt[Sum[(i - 1 - μ)^2 y[[i]], {i, 1, 4}]]
(* 0.793748 *)

Notes on your code:

In Mathematica indices start at 1, not 0. Furthermore, you need to use y[[i]] to refer to ith Part of a list y, not y[i]. With these changes

Sqrt[\!\( \*SubsuperscriptBox[\(∑\), 
 \(i = 1\), \(4\)]\( \*SuperscriptBox[\((i - 1 - μ)\), 
 \(2\)]\ \*SubscriptBox[\(y\), \([\([i]\)]\)]\)\)]

also gives 0.793748.

Finally, if you have to, you can use part indices as subscripts using, for example, Esc[[Esc 2 Esc]]Esc for Part 2.


If what you wish to calculate is $\sqrt{\sum _{i=0}^3 (i-\mu )^2 y_i}$, this can be done:

Sqrt@Total[(Range[0, 3] - μ)^2 y]

which gives 0.793748. The function Range[0,3] gives the numbers {0,1,2,3}, each of which is subtracted from mu and squared. These are then multiplied by the corresponding element of y and the collection is summed using Total.

  • $\begingroup$ Thanks, that's good to know. Not the answer to the question, which is about how to access list elements via index, but I've no doubt I'll be using Range and Total in the future $\endgroup$
    – Ben
    Apr 19, 2015 at 23:37
  • $\begingroup$ @Ben -- I think the point is that it is often better (when programing in Mathematica) to not express formulas in an index-by-index fashion, but rather to use operations on the vectors/lists. $\endgroup$
    – bill s
    Apr 21, 2015 at 3:03
  • $\begingroup$ No doubt that's true. And I anticipate vectorizing my solutions and programming in a generally functional style when writing full-blown programs. For the time being, though, my purpose is to use the symbolic tools here to learn calculus (and statistics), not to optimize my code. $\endgroup$
    – Ben
    Apr 21, 2015 at 23:55

Just another way to index (not as concise as kguler) but may be useful to note is MapIndexed:

Sqrt[Total @ MapIndexed[(#2[[1]] - 1 - μ)^2 #1 &, y]]

yielding 0.793748

  • #2 is the index starting at {1}, hence need for #2[[1]]
  • lists/arrays start at 1, so reindexing -> #2[[1]]-1 as per kguler

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.