Finding the average of multiple arrays

I have:

$array->[0]->[0..4] = (1,2,3,4,5)

$array->[1]->[0..6] = (12,13,14,15,16,17,18)

$array->[2]->[0..8] = (4,5,6,7,8,9,10,11,12)

I want to find the average for each of the arrays so the result is:

$array->[0] = 3

$array->[1] = 15

$array->[2] = 8

This is just a sample of what my data looks like but I really have arrays 0 to 72-510 with each array containing 0 to 1-40 elements. My current script is set up to where I have the variable $i ticking through the total number of arrays and variable $j that ticks through the number of elements in the arrays.

Below is the basic set up of my script.

my $sum;
my $value;

foreach (my $i = 0; $i < "Number of Arrays"; ++$i) { 
 for (my $j = 0; $j < "Size of Array"; ++$j) { 
     $value = $array->[$i]->[$j];
 }
 $sum += $values;
 my $average = $sum / "Size of Array"; 
}

If anyone can show me an example of how to accomplish this with the example that would be really helpful! If I find anything in the mean time I will update my question.

Upvotes: 3

Views: 527

Answers (4)

Govind
Govind

Reputation: 444

Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) 
        [1] => Array ( [0] => 12 [1] => 13 [2] => 14 [3] => 15 [4] => 16 [5] => 17 [6] => 18 ) 
        [2] => Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 [4] => 8 [5] => 9 [6] => 10 [7] => 11 [8] => 12 ) )

output:

Array([0] => 3.5
      [1] => 15
      [2] => 8)

The code as follows:

    $sum = 0;
    $count = 0;
    foreach ($arrays as $key => $value) {
        foreach ($value as $k => $v) {
            $sum += $v;
            $count++;
        }
        $avg_array[$key] = $sum / $count;
        $sum = 0;
        $count = 0;
    }
    print_r($avg_array);

I hope this will solve your problem.

Upvotes: 0

Benjamin W.
Benjamin W.

Reputation: 52251

Using List::Util and no C-style for-loops:

use strict;
use warnings;
use List::Util qw(sum);
use feature 'say';

my $array = [
    [1, 2, 3, 4, 5],
    [12, 13, 14, 15, 16, 17, 18],
    [4, 5, 6, 7, 8, 9, 10, 11, 12],
    []
];

foreach my $sub_array (@$array) {
    @$sub_array or next;                       # Is the array empty?
    my $avg = sum(@$sub_array) / @$sub_array;
    say "[@$sub_array] has average $avg";
}

Upvotes: 3

Vasile Felnecan
Vasile Felnecan

Reputation: 16

Given that you have the following php arrays (from your example)

$arrayOfAllArrays = array(
   'first_array' => array(1,2,3,4,5),
   'second_array' => array(12,13,14,15,16,17,18),
   'third_array' => array(4,5,6,7,8,9,10,11,12)
);

You can use the following script (I used long variables to make it easier for you to understand):

$arrayOfAverages = array();
foreach ($arrayOfAllArrays as $key => $array) {
    $sumOfArrayElements = array_sum($array);
    $numberOfElementsInArray = count($array);
    $arrayOfAverages[$key] = $sumOfArrayElements / $numberOfElementsInArray;
}

And you will have the averages stored in the array $arrayOfAverages. This is a print for the array mentioned:

Array
(
    [first_array] => 3
    [second_array] => 15
    [third_array] => 8
)

Upvotes: 0

Paul Roub
Paul Roub

Reputation: 36448

You're in the ballpark.

You want to re-initialize $sum to 0 for each array, and add every $value (not $values) to $sum:

foreach (my $i = 0; $i < "Number of Arrays"; ++$i) { 
  my $sum = 0;

  for (my $j = 0; $j < "Size of Array"; ++$j) { 
    my $value = $array->[$i]->[$j];
    $sum += $value;
  }

  my $average = $sum / "Size of Array"; 
}

Upvotes: 1

Related Questions