Last active
June 14, 2016 02:57
-
-
Save Sibirtsev/0c3bff023bc3a717878e786185925eed to your computer and use it in GitHub Desktop.
Interpolation matrix of numbers with mean values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php scale.php | |
[5 x 5] | |
13.00 11.00 13.00 15.00 10.00 | |
12.00 10.00 15.00 11.00 13.00 | |
10.00 15.00 10.00 12.00 14.00 | |
15.00 11.00 13.00 11.00 14.00 | |
15.00 13.00 15.00 12.00 0.00 | |
[9 x 9] | |
13.00 12.00 11.00 12.00 13.00 14.00 15.00 12.50 10.00 | |
12.50 11.50 10.50 12.25 14.00 13.50 13.00 12.25 11.50 | |
12.00 11.00 10.00 12.50 15.00 13.00 11.00 12.00 13.00 | |
11.00 11.75 12.50 12.50 12.50 12.00 11.50 12.50 13.50 | |
10.00 12.50 15.00 12.50 10.00 11.00 12.00 13.00 14.00 | |
12.50 12.75 13.00 12.25 11.50 11.50 11.50 12.75 14.00 | |
15.00 13.00 11.00 12.00 13.00 12.00 11.00 12.50 14.00 | |
15.00 13.50 12.00 13.00 14.00 12.75 11.50 9.25 7.00 | |
15.00 14.00 13.00 14.00 15.00 13.50 12.00 6.00 0.00 | |
[17 x 17] | |
13.00 12.50 12.00 11.50 11.00 11.50 12.00 12.50 13.00 13.50 14.00 14.50 15.00 13.75 12.50 11.25 10.00 | |
12.75 12.25 11.75 11.25 10.75 11.44 12.12 12.81 13.50 13.62 13.75 13.88 14.00 13.19 12.38 11.56 10.75 | |
12.50 12.00 11.50 11.00 10.50 11.38 12.25 13.12 14.00 13.75 13.50 13.25 13.00 12.62 12.25 11.88 11.50 | |
12.25 11.75 11.25 10.75 10.25 11.31 12.38 13.44 14.50 13.88 13.25 12.62 12.00 12.06 12.12 12.19 12.25 | |
12.00 11.50 11.00 10.50 10.00 11.25 12.50 13.75 15.00 14.00 13.00 12.00 11.00 11.50 12.00 12.50 13.00 | |
11.50 11.44 11.38 11.31 11.25 11.88 12.50 13.12 13.75 13.12 12.50 11.88 11.25 11.75 12.25 12.75 13.25 | |
11.00 11.38 11.75 12.12 12.50 12.50 12.50 12.50 12.50 12.25 12.00 11.75 11.50 12.00 12.50 13.00 13.50 | |
10.50 11.31 12.12 12.94 13.75 13.12 12.50 11.88 11.25 11.38 11.50 11.62 11.75 12.25 12.75 13.25 13.75 | |
10.00 11.25 12.50 13.75 15.00 13.75 12.50 11.25 10.00 10.50 11.00 11.50 12.00 12.50 13.00 13.50 14.00 | |
11.25 11.94 12.62 13.31 14.00 13.19 12.38 11.56 10.75 11.00 11.25 11.50 11.75 12.31 12.88 13.44 14.00 | |
12.50 12.62 12.75 12.88 13.00 12.62 12.25 11.88 11.50 11.50 11.50 11.50 11.50 12.12 12.75 13.38 14.00 | |
13.75 13.31 12.88 12.44 12.00 12.06 12.12 12.19 12.25 12.00 11.75 11.50 11.25 11.94 12.62 13.31 14.00 | |
15.00 14.00 13.00 12.00 11.00 11.50 12.00 12.50 13.00 12.50 12.00 11.50 11.00 11.75 12.50 13.25 14.00 | |
15.00 14.12 13.25 12.38 11.50 12.00 12.50 13.00 13.50 12.94 12.38 11.81 11.25 11.06 10.88 10.69 10.50 | |
15.00 14.25 13.50 12.75 12.00 12.50 13.00 13.50 14.00 13.38 12.75 12.12 11.50 10.38 9.25 8.12 7.00 | |
15.00 14.38 13.75 13.12 12.50 13.00 13.50 14.00 14.50 13.81 13.12 12.44 11.75 9.69 7.62 5.56 3.50 | |
15.00 14.50 14.00 13.50 13.00 13.50 14.00 14.50 15.00 14.25 13.50 12.75 12.00 9.00 6.00 3.00 0.00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function display($data, array $size) { | |
print PHP_EOL . '[' . $size[0] . ' x ' . $size[1] . ']' . PHP_EOL; | |
$idx = 0; | |
for ($i = 0; $i < $size[0]; $i++) { | |
for ($j = 0; $j < $size[1]; $j++) { | |
printf("%5.2f\t", $data[$idx]); | |
$idx++; | |
} | |
print PHP_EOL; | |
} | |
print PHP_EOL; | |
} | |
function create_data(array $size = [2,2], $min = 0, $max = 1, $rand_cut = true) { | |
$cut = (($rand_cut) ? rand(1, $size[1] / 2) : 0); | |
$plain_size = $size[0] * $size[1] - $cut; | |
$data = []; | |
for ($i = 0; $i < $plain_size; $i++) { | |
$data[$i] = rand(min([$min, $max]), max([$min, $max])); | |
} | |
for ($j = $i; $j < $plain_size + $cut; $j++) { | |
$data[$j] = 0; | |
} | |
return [$data, $size]; | |
} | |
function avg(array $els) { | |
if (count($els) == 0) return 0; | |
return array_sum($els) / count($els); | |
} | |
function array_matrix($data, array $size) { | |
$matrix = []; | |
$idx = 0; | |
for ($row = 0; $row < $size[0]; $row++) { | |
for ($column = 0; $column < $size[1]; $column++) { | |
$matrix[$row][$column] = $data[$idx]; | |
$idx++; | |
} | |
} | |
return $matrix; | |
} | |
function array_flatten($array, $return) { | |
for ($i = 0; $i < count($array); $i++) { | |
if (is_array($array[$i])) { | |
$return = array_flatten($array[$i], $return); | |
} else { | |
if (isset($array[$i])) { | |
$return[] = $array[$i]; | |
} | |
} | |
} | |
return $return; | |
} | |
function scale($data, array $size) { | |
$matrix = array_matrix($data, $size); | |
$new_matrix = []; | |
for ($row = 0; $row < $size[0]; $row++) { | |
for ($column = 0; $column < $size[1]; $column++) { | |
if ($column == 0) { | |
$new_matrix[$row][] = $matrix[$row][$column]; | |
continue; | |
} | |
$new_matrix[$row][] = avg([$matrix[$row][$column - 1], $matrix[$row][$column]]); | |
$new_matrix[$row][] = $matrix[$row][$column]; | |
} | |
} | |
$matrix = $new_matrix; | |
$new_matrix = []; | |
for ($row = 0; $row < $size[0]; $row++) { | |
if ($row == 0) { | |
$new_matrix[] = $matrix[0]; | |
continue; | |
} | |
$new_row = []; | |
for ($column = 0; $column < count($matrix[$row]); $column++) { | |
$new_row[] = avg([$matrix[$row - 1][$column], $matrix[$row][$column]]); | |
} | |
$new_matrix[] = $new_row; | |
$new_matrix[] = $matrix[$row]; | |
} | |
return [array_flatten($new_matrix, []), [count($new_matrix), count($new_matrix[0])]]; | |
} | |
$data = create_data([5,5], 10, 15); | |
display($data[0], $data[1]); | |
$data = scale($data[0], $data[1]); | |
display($data[0], $data[1]); | |
$data = scale($data[0], $data[1]); | |
display($data[0], $data[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment