Finding the average of 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 do have arrays from 0 to 72-510 with each array containing 0 to 1-40 elements. My current script is set to where I have my $ i variable ticking through the total number of arrays and the $ j variable ticking through the number of elements in the arrays.
Below is the basic setup 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 on how to do this with an example that would be really helpful! If I find anything in the middle, I'll update my question.
source to share
You are one step.
You want to reinitialize $sum
before 0
for every array and add everything $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";
}
source to share
With List::Util
and not for 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";
}
source to share
Considering 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 averages stored in $ arrayOfAverages. This is the print for the mentioned array:
Array
(
[first_array] => 3
[second_array] => 15
[third_array] => 8
)
source to share
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 looks like this:
$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 solves your problem.
source to share