Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //----------------------------
- //-- matrix multiplication
- //-- 120925 cahyadsn@yahoo.com
- //-----------------------------
- $a = array(array(2,1),array(4,5),array(6,7));
- print_matrix($a,$title='MATRIK A');
- $b = array(array(2,3,2),array(1,1,-3));
- print_matrix($b,$title='MATRIK B');
- if(count($a[0])==count($b)){
- for($i=0;$i<count($a);$i++){
- for($j=0;$j<count($b[0]);$j++){
- $c[$i][$j]=0;
- for($k=0;$k<count($b);$k++){
- $c[$i][$j]+=$a[$i][$k]*$b[$k][$j];
- }
- }
- }
- print_matrix($c,$title='MATRIK C (AB)');
- }else{
- echo "jumlah kolom matrik pertama != jumlah baris matrik kedua";
- }
- function print_matrix($mat,$title=''){
- echo "<pre>$title=\n";
- for($i=0;$i<count($mat);$i++){
- for($j=0;$j<count($mat[0]);$j++){
- echo str_pad($mat[$i][$j],5,' ',STR_PAD_BOTH);
- }
- echo "\n";
- }
- echo "</pre>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement