0
$\begingroup$

Hello I just started using Mathematica, and I want to create a mean function without using the built-in command.

I did that so far, but the code is not running :

mean[list_] := (
  For[i = 0, i < Length[list], i++,
   mean = (Sum[list[n], {n, 1, i}])/Length[list]])

If anyone has an idea, thanks for the future help.

$\endgroup$
3
  • $\begingroup$ I suppose the For loop isn't really necessary, since Sum adds up over the list. You could use Total: mean[list_] := Total[list]/Length[list] $\endgroup$
    – Chris K
    Apr 17, 2020 at 21:43
  • $\begingroup$ Thank a lot for you help ! $\endgroup$ Apr 17, 2020 at 21:45
  • 1
    $\begingroup$ I think f[_]:="I am going to bite you" is a pretty mean function (just saying). $\endgroup$ Apr 18, 2020 at 14:51

2 Answers 2

5
$\begingroup$

There are a few things wrong with your code:

mean[list_] := (
  For[i = 0, i < Length[list], i++,
   mean = (Sum[list[n], {n, 1, i}])/Length[list]])

Let's clear the incorrect definition:

Clear[mean]
  1. You do not need the parenthesis around the For[] loop since this is the only command:
mean[list_] := For[i = 0, i < Length[list], i++, mean = (Sum[list[n], {n, 1, i}])/Length[list]]
  1. You are defining a function mean[list_], but you also redefine mean inside your function. That will not work, so let's call the other one mean2:
mean[list_] := For[i = 0, i < Length[list], i++, mean2 = (Sum[list[n], {n, 1, i}])/Length[list]]
  1. A For loop does not return a value in the Wolfram Language. I think you want to return mean2 here, so let's add that. This turns the function into multiple statements which are separated by a semi-colon. Also because we now have multiple statements we need to use parenthesis:
mean[list_] := (
 For[i = 0, i < Length[list], i++, mean2 = (Sum[list[n], {n, 1, i}])/Length[list]];
 mean2)
  1. Since mean2 and i are local temporary variable it is best to declare them as such using a Module:
mean[list_] := Module[{mean2,i},
 For[i = 0, i < Length[list], i++, mean2 = (Sum[list[n], {n, 1, i}])/Length[list]];
 mean2
]
  1. Another programming error is the use of list[n] where you mean to use list[[n]] (which is the short notation for Part[list,n]):
mean[list_] := Module[{mean2,i},
 For[i = 0, i < Length[list], i++, mean2 = (Sum[list[[n]], {n, 1, i}])/Length[list]];
 mean2
]
  1. At this point you have something that approaches normal code, however it gives the wrong answer, because the For loop stops when i is one less than the length of the list. You can fix that by using <= instead of < . Also list indices start with 1 in the Wolfram Language:
mean[list_] := Module[{mean2,i},
 For[i = 1, i <= Length[list], i++, mean2 = (Sum[list[[n]], {n, 1, i}])/Length[list]];
 mean2
]
  1. At this point you get the right answer, but you are doing too much computational work. Specifically you are computing mean2 many times, but only the last one gives the mean value. So the whole For loop is not required. And when you remove the For loop you also don't need the temporary variables and you also don't need the Module, so it becomes just:
mean[list_] := Sum[list[[n]], {n, 1, Length[list]}] / Length[list]
  1. A simpler way to sum the elements of a list is to use Total:
mean[list_] := Total[list] / Length[list]
  1. Or more directly you can use Mean:
mean[list_] := Mean[list]
  1. If you really want to use a For loop, you can use something like this:
mean[list_] := Module[{total = 0},
  For[i = 1, i <= Length[list], i++, total += list[[i]]];
  total/Length[list]
  ]

(Note that in the Wolfram Language lists are indexed starting with 1, not 0.)

$\endgroup$
2
$\begingroup$

Two variations based off your code

mean[list_] := Module[
  {accumulator = 0, len = Length[list]},
  For[i = 1, i <= len, i++,
   accumulator += list[[i]]];
  accumulator/len]

Clear[mean]

mean[list_] :=
 Sum[list[[i]], {i, 1, Length[list]}]/Length[list]
$\endgroup$

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.