0

Although the order of matrices should be fine, the following code throws back the exception. It might be a tiny thing I'm not being able to notice, but can't figure it out.

<?php $mat1 = array(5,1); $mat2 = array(1,5); function matrixmult($m1,$m2){ $r=count($m1); $c=count($m2[0]); $p=count($m2); if(count($m1[0])!=$p){throw new Exception('Incompatible matrixes');} $m3=array(); for ($i=0;$i< $r;$i++){ for($j=0;$j<$c;$j++){ $m3[$i][$j]=0; for($k=0;$k<$p;$k++){ $m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j]; } } } } return($m3); } matrixmult($mat1,$mat2); ?> 
3
  • You shouldn't be able to multiply a 1x2 matrix by a 1x2 matrix; $mat2 should be a 2x1 matrix: $mat2 = array(array(1),array(5)); Commented Aug 9, 2014 at 16:31
  • 2
    Mark, He's also missing a wrapping array( ) around them. He's essentially defining vectors here, those aren't two-dimensional arrays. Commented Aug 9, 2014 at 16:35
  • See also: Matrix artihmetic in PHP? Commented Aug 27, 2014 at 23:27

3 Answers 3

1

You're specifying your test matrices wrong, in two ways:

  1. The arrays aren't two-dimensional (that is, arrays of arrays of numbers).
  2. Even if you wrapped another array( ) around them, the condition that the width of the first matrix be equal to the height of the second matrix doesn't hold with [5 1] and [1 5], which are both 2 wide and 1 high.

What you need is something like

$mat1 = array(array(5,1)); $mat2 = array(array(1),array(5)); 
Sign up to request clarification or add additional context in comments.

2 Comments

How do you access elements within ? Its not multidimensional array, so is it not making things more complex?
Yes, those are multidimensional. Try var_dump()ing them to see a slightly better representation. On how to access elements in a multidimensional array, inspect the $m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j]; line in your code.
1

Just to round this out. Here is a working solution:

function M_mult($_A,$_B) { // AxB outcome is C with A's rows and B'c cols $r = count($_A); $c = count($_B[0]); $in= count($_B); // or $_A[0]. $in is 'inner' count if ( $in != count($_A[0]) ) { print("ERROR: need to have inner size of matrices match.\n"); print(" : trying to multiply a ".count($_A)."x".count($_A[0])." by a ".count($_B)."x".count($_B[0])." matrix.\n"); print("\n"); exit(1); } // allocate retval $retval = array(); for($i=0;$i< $r; $i++) { $retval[$i] = array(); } // multiplication here for($ri=0;$ri<$r;$ri++) { for($ci=0;$ci<$c;$ci++) { $retval[$ri][$ci] = 0.0; for($j=0;$j<$in;$j++) { $retval[$ri][$ci] += $_A[$ri][$j] * $_B[$j][$ci]; } } } return $retval; } } 

Comments

0

You must delete the curly braces before return.

<?php $mat1 = array(5,1); $mat2 = array(1,5); function matrixmult($m1,$m2){ $r=count($m1); $c=count($m2[0]); $p=count($m2); if(count($m1[0])!=$p){throw new Exception('Incompatible matrixes');} $m3=array(); for ($i=0;$i< $r;$i++){ for($j=0;$j<$c;$j++){ $m3[$i][$j]=0; for($k=0;$k<$p;$k++){ $m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j]; } } } return($m3); } matrixmult($mat1,$mat2); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.