Reputation: 31
I have problem with matrix operation in php.
I have two multidimensional array
array1=
Array([0] => Array([0] => 5000, [1] => 6, [2] => 325, [3] => 3, [4] => 3, [5] => 517000000)
[1] => Array([0] => 20000, [1] => 5, [2] => 217, [3] => 5, [4] => 3, [5] => 1692000000)
[2] => Array([0] => 12150, [1] => 3, [2] => 254, [3] => 4, [4] => 4, [5] => 1370520000)
[3] => Array([0] => 4200, [1] => 4, [2] => 351, [3] => 3, [4] => 2, [5] => 394800000)
[4] => Array([0] => 24700, [1] => 7, [2] => 237, [3] => 3, [4] => 4, [5] => 2089620000)
[5] => Array([0] => 18500, [1] => 5, [2] => 314, [3] => 5, [4] => 4, [5] => 1739000000)
[6] => Array([0] => 12150, [1] => 5, [2] => 247, [3] => 3, [4] => 3, [5] => 1142100000)
[7] => Array([0] => 15000, [1] => 5, [2] => 307, [3] => 4, [4] => 3, [5] => 1410000000)
[8] => Array([0] => 10000, [1] => 4, [2] => 231, [3] => 4, [4] => 4, [5] => 940000000)
[9] => Array([0] => 27500, [1] => 6, [2] => 347, [3] => 5, [4] => 4, [5] => 2147483647))
the table display is
5000 6 325 3 3 517000000
20000 5 217 5 3 1692000000
12150 3 254 4 4 1370520000
4200 4 351 3 2 394800000
24700 7 237 3 4 2089620000
18500 5 314 5 4 1739000000
12150 5 247 3 3 1142100000
15000 5 307 4 3 1410000000
10000 4 231 4 4 940000000
27500 6 347 5 4 2147483647
and the second array is
array2=
Array(
[0] => Array
(
[0] => 27500
[1] => 7
[2] => 351
[3] => 3
[4] => 4
[5] => 394800000
))
then, I want to do mathematical operation for each column.
for column 0,1,2,4 the operation is array1[][]/array2[][].
and for column 3 and 5 the operation is array2[][]/array1[][]
how do I do that ?
I have tried for this function :
function perkalian_matriks($array1, $array2) {
$result= array();
for ($i=0; $i<sizeof($array1); $i++) {
for ($j=0; $j<sizeof($array2[0]); $j++) {
$temp = 0;
$temp1 = 0;
$temp2 = 0;
for ($k=0; $k<sizeof($array2); $k++) {
if(isset($array1[$i][3]) || isset($array1[$i][5])){
$temp1 += ($array2[0][$j]/$array1[$i][$j])/2;
}else{
$temp2 += $array1[$i][$j]/$array2[0][$j];
}
$temp += $array1[$i][$j]/$array2[0][$j];
}
$result[$i][$j] = $temp;
$result1[$i][$j] = $temp1;
$result2[$i][$j] = $temp2;
}
}
return $result;
return $temp;
return $result1;
return $result2;
}
But the final result that I expected is
0.181818 0.857143 0.925926 1 0.75 0.763636
0.727273 0.714286 0.618234 0.6 0.75 0.233333
0.441818 0.428571 0.723647 0.75 1 0.288066
0.152727 0.571429 1 1 0.5 1
0.898182 1 0.675214 1 1 0.188934
0.672727 0.714286 0.894587 0.6 1 0.227027
0.441818 0.714286 0.703704 1 0.75 0.345679
0.545455 0.714286 0.874644 0.75 0.75 0.28
0.363636 0.571429 0.65812 0.75 1 0.42
1 0.857143 0.988604 0.6 1 0.183843
Thank you.
Upvotes: 0
Views: 48
Reputation: 5574
first of all doing this:
return $result;
return $temp;
return $result1;
return $result2;
will execute every time just return $result;
so that really ve no meaning.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. ( from php doc )
The second array don't need to be a matrix.
anyway you can achieve what you need using 2 nested loop:
$array1=
array(array(5000,6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000),
array( 12150, 3, 254, 4, 4, 1370520000),
array( 4200, 4, 351, 3, 2, 394800000),
array( 24700, 7, 237, 3, 4, 2089620000),
array( 18500, 5, 314, 5, 4, 1739000000),
array( 12150, 5, 247, 3, 3, 1142100000),
array( 15000, 5, 307, 4, 3, 1410000000),
array( 10000, 4, 231, 4, 4, 940000000),
array( 27500, 6, 347, 5, 4, 2147483647));
$array2=array(27500,
7,
351,
3,
4,
394800000);
$action1 = array(0,1,2,4);
$action2 = array(3,5);
$result = array();
foreach($array1 as $rowNum => $row){
$result[] = array();
foreach($row as $colNum => $col){
if(in_array($colNum,$action1))
$result[$rowNum][] = $col/$array2[$colNum];
else if(in_array($colNum,$action2)){
$result[$rowNum][] = $array2[$colNum]/$col;
}
}
}
echo var_dump($result);
Upvotes: 1