title: Averaging part of an array powershell
body: <p>Hi I am trying to return the average for part of an array, when the array is set out like </p>
  
  <pre><code>$multi = New-Object 'object[,]' $nucount,($readings + 2 )
  and contains "1,4,2,6,3,4,5,nameofitem, cost of item"
  </code></pre>
  
  <p>I want to get an average for the first 7 elements which I know will always be the first 7 and always be a number. In the case above the number of elements containing a number will be held in a variable called $readings. So the sum I want to do is add up the elements in the array up to the $readings value. and then device by $readings. </p>
  
  <p>I know of ways like this for the whole array</p>
  
  <pre><code>$Avg = ($array | Measure-Object -Average);
  $Avg.Average;
  </code></pre>
  
  <p>or simple looping through and calculating the average buy adding and deviding. </p>
  
  <p>But is there any short hand way to do this. I was going to used the ... operator </p>
  
  <p>$multi[2,0..$readings] but i get an error of </p>
  
  <blockquote>
    <p>Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32".</p>
  </blockquote>
  
  <p>Any help with this would be great if it can provide a short code to replace the </p>
  
  <pre><code>for (x=1 ; $x -eq $readings ; $X++) 
  {
  $sum = $sum + $multi[2,$x]
  {
  $avg= $sum/$readings
  </code></pre>