Jump to content

Matrix issue


firedrop84

Recommended Posts

Hi Everyone,

 

I have a problem in regards the coding that I am trying to develop. I am trying to implement some concept which uses matrix but I didn't find any matrix function in php which does that.

 

I done some calculation which is working fine and now I have two arrays which I need to multiplie the first and the second array and get a new array.

 

I have to do it twice for a two dimminssional array and a single diminssioanl array.

 

Can any help advice how this can be done.

 

p.s. I already found this one site which has a class for it but it's quite complex. Any other site you know about it or any ideas you can give that might help http://www.phpclasses.org/browse/package/2859.html.

 

 

Your advice is greatly approciated.

 

Link to comment
https://forums.phpfreaks.com/topic/196332-matrix-issue/
Share on other sites

Thanks Daniel, your support is greatly approciated and I really need it.

 

I am studying my masters but I am from Information System background not Computer Science which they gave us almost no math in uni. Matrix is a new concept for me. However, I asked a guy who graduated in math and he explained it for me. He doesn't have any IT knowledge. Based on what he said, I developed the sheet attached.

 

The stuff I wrote in red in the attachment it's really just the index that it will be in the array. I thought if I have two arrays and each as two colomuns and two rows. Then, I thought maybe then I need to divide them into four arrays which they are C11, C12, C21, C22. You will see with the black pen is where I am getting the elements I need from each array. Then, I finish it by having a final array by adding the result into a final array.

 

hmmm, nested for loop. Let me try to think how can I do it.

 

to make my question clearler . This is a link of a thesis. Please look at page 294 - 299 which explains what I am trying to do. http://www.debii.curtin.edu.au/~ponny/doc/PhD_THESIS.pdf

 

:confused:

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1031682
Share on other sites

Ah, well, it's pretty easy actually:

 

<?php
function matrixMult(array $m1, array $m2)
{
$m1rows = count($m1);
$m1cols = count($m1[0]);
$m2cols = count($m2[0]);

for ($i = 0; $i < $m1rows; ++$i) {
	for ($j = 0; $j < $m2cols; ++$j) {
		for ($k = 0; $k < $m1cols; ++$k) {
			if (!isset($m3[$i]))
				$m3[$i] = array();
			if (!isset($m3[$i][$j]))
				$m3[$i][$j] = 0;

			$m3[$i][$j] +=  $m1[$i][$k] * $m2[$k][$j];
		}
	}
}

return $m3;
}

$m1 = array(
array(1, 2),
array(3, 4),
array(5, 6),
);

$m2 = array(
array(1, 2, 3),
array(4, 5, 6),
);

print_r(matrixMult($m1, $m2));

Link to comment
https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1031691
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.