Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function show_matrix($m)
- {
- $row=count($m);
- $col=count($m[0]);
- for($i=0;$i<$row;$i++)
- {
- if($col!=1)
- {
- for($j=0;$j<$col;$j++)
- {
- echo str_pad(round($m[$i][$j],5),12,' ',STR_PAD_BOTH);
- }
- }
- else
- {
- echo str_pad(round($m[$i],5),12,' ',STR_PAD_BOTH);
- }
- echo "\n";
- }
- }
- function process_2($a,$b)
- {
- $row=count($a);
- $col=count($b[0]);
- $com=count($b);
- if(count($a[0])!=$com){throw new Exception('Incompatible matrixes');}
- if($col!=1){
- for ($i=0;$i<$row;$i++)
- {
- for ($j=0;$j<$col;$j++)
- {
- $result[$i][$j]=0;
- for($k=0;$k<$com;$k++){
- $result[$i][$j] += POW($a[$i][$k]-$b[$k][$j],2);
- }
- $result[$i][$j]=SQRT($result[$i][$j]);
- echo $result[$i][$j]."\n";
- }
- }
- }
- else
- {
- for ($i=0;$i<$row;$i++)
- {
- $result[$i]=0;
- for($k=0;$k<$com;$k++){
- $result[$i] += POW($a[$i][$k]-$b[$k],2);
- echo "POW(".$a[$i][$k]."-".$b[$k].",2)=".POW($a[$i][$k]-$b[$k],2)."+";
- }
- $result[$i]=SQRT($result[$i]);
- echo "==>".$result[$i]."\n";
- }
- }
- return $result;
- }
- $data1 = array(
- array( 2.1822, 4.36435, 1.0911),
- array(1.20604, 1.20604, 3.61812),
- array(1.7056, 2.5584, 2.5584)
- );
- $data2 = array(4.36435, 3.61812, 2.5584);
- echo "<pre>Proses\n";
- $hasil=process_2($data1,$data2);
- echo "\ndata1\n";
- show_matrix($data1);
- echo "\ndata2\n";
- show_matrix($data2);
- echo "\nhasil\n";
- show_matrix($hasil);
- echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement