-
-
Save catwhocode/473c0c081e7d5cd9ba36f12b02230082 to your computer and use it in GitHub Desktop.
[PHP] Fungsi Perkalian Matriks
This file contains hidden or 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 | |
/* Fungsi perkalian matriks | |
* @f2face - 2013 | |
*/ | |
function kalikan_matriks($matriks_a, $matriks_b) { | |
$hasil = array(); | |
for ($i = 0; $i < sizeof($matriks_a); $i++) { | |
for ($j = 0; $j < sizeof($matriks_b[0]); $j++) { | |
$temp = 0; | |
for ($k = 0; $k < sizeof($matriks_b); $k++) { | |
$temp += $matriks_a[$i][$k] * $matriks_b[$k][$j]; | |
} | |
$hasil[$i][$j] = $temp; | |
} | |
} | |
return $hasil; | |
} | |
// matriks A | |
$a = []; | |
$a[] = [1, 2, 3]; | |
$a[] = [4, 5, 6]; | |
$a[] = [7, 8, 9]; | |
$a[] = [10, 11, 12]; | |
// Matriks B | |
$b = []; | |
$b[] = [1, 2, 3, 4]; | |
$b[] = [5, 6, 7, 8]; | |
$b[] = [9, 10, 11, 12]; | |
// Kalikan | |
$hasil = kalikan_matriks($a, $b); | |
?> | |
<table cellspacing="0" cellpadding="10" border="1"> | |
<tr> | |
<td> | |
<?php | |
echo '<h3>Matriks A:</h3>'; | |
echo '<pre>'; | |
print_r($a); | |
echo '</pre>'; | |
?> | |
</td> | |
<td valign="top"> | |
<?php | |
echo '<h3>Matriks B:</h3>'; | |
echo '<pre>'; | |
print_r($b); | |
echo '</pre>'; | |
?> | |
</td> | |
<td> | |
<h3>Hasil Perkalian Matriks A x B</h3> | |
<table border='1' cellspacing='0' cellpadding='50'> | |
<?php | |
for ($i = 0; $i < sizeof($hasil); $i++) { | |
echo "<tr>"; | |
for ($j = 0; $j < sizeof($hasil[$i]); $j++) { | |
echo "<td>" . round($hasil[$i][$j], 4) . "</td>"; | |
} | |
echo "</tr>"; | |
} | |
?> | |
</table> | |
</td> | |
</tr> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment