Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to use the Total function on a list that includes negative values, and can't seem to figure out how to stop Mathematica from getting hung up on those values.

The Total function is part of a Do loop;

Do[index = (Ca[[i]] - Mean[Ca])*(Cb[[i]] - Mean[Cb]); (Total[index])/40, {i, 40}]

I get the following messages:

Total::normal: Nonatomic expression expected at position 1 in Total[-47.3332].

Total::normal: Nonatomic expression expected at position 1 in Total[-25.6954].

Total::normal: Nonatomic expression expected at position 1 in Total[-0.779181].

General::stop: Further output of Total::normal will be suppressed during this calculation.

I know that further down the index list there are positive values, which cause problems trying to use the solutions from the "Counting negative values in list" thread. I'm terribly sorry for such a beginner question, but I'm extremely hung up on this problem!


Ah, thank you so much Jonie, that was an increadibly beginner flaw in my loop design. I fixed it to

Lista = {} 
Do[index = (Ca[[i]] - Mean[Ca])*(Cb[[i]] - Mean[Cb]);   AppendTo[Lista, index], {i, 40}]  

And it works fine now. Much appreciate you taking the time to point that out!

share|improve this question

closed as off-topic by m_goldberg, Kuba, Sjoerd C. de Vries, Artes, Mr.Wizard Jul 22 '13 at 14:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Kuba, Sjoerd C. de Vries, Artes, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

The problem isn't about Total being used on a negative value. It's because index is not a list of values so there's nothing to Total on. Hence you got the errors saying its expecting a nonatomic expression (expressions such as lists). –  Jonie Jul 22 '13 at 1:30
Give this a try Total[(Ca - Mean[Ca]) * (Cb - Mean[Cb])] –  Jonie Jul 22 '13 at 1:36

1 Answer 1

up vote 4 down vote accepted

This could just be done as inner product of two lists (vectors):

share|improve this answer
Oh. Well, that's what I get for being so hung up on using loops! Thank you very much, that's indeed much easier. –  Rel Jul 22 '13 at 2:14
Mathematica allows to you achieve your goal in mumber of ways. Just thought this was a neat compact one. –  ubpdqn Jul 22 '13 at 3:00

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