Codeigniter:如何从db中乘以矩阵数组数据?

well i have a problem with logic of matrices multiplication in php; the data comes from the database in the form of an one dimension array (Array ( [0] => 1.0000 [1] => 0.5000 [2] => 3.0000 [3] => 2.0000 [4] => 1.0000 [5] => 5.0000 [6] => 0.3333 [7] => 0.2000 [8] => 1.0000 ) ), that I need to transform into a matrice. The dimension of the original array is a square number (in this case 9), so the result matrice will have two equal dimensions, both equal to the square root (3) of the original data array. The result matrice has to be multiplied by itself, using the pattern in the image below: enter image description here

I have made some research before, but none of them were right.

i have the following code i used in the model to create the algorithm:

function hitung_matriks(){ 
     $query = $this->db->query("select * from banding b 
      inner join kriteria a on a.Kd_Kriteria1 = b.Kd_Kriteria1");
     $dt_matriks = $query->result();

     $data = array();
    foreach($dt_matriks as $a){
        $data[] = $a->Nilai_Banding;
    }

    echo "<pre>";
    print_r($data);
    echo "</pre>";

    $c = array();
    for($i=1;$i<=sqrt(count($data));$i++){
       $d = array();
        $isi=0;
        for($j=1;$j<=sqrt(count($data));$j++){
            $isi = $data[$i][$j] * $data[$j][$i];
            $d[] = $isi;
        }

        $c[] = $d;
    }
    echo "<pre>";
    print_r($c);
    echo "</pre>";die();
}

and the result of each array comes 0.

I want to make this code works to be like this :

img

please help me :'(

Updated answer: Way to transform $data array into a matrice:

$data = array(1.0000, 0.5000, 3.0000, 2.0000, 1.0000, 5.0000, 0.3333, 0.2000, 1.0000);
$data2 = array();
$j = 0;
$k = 0;
for($i=0;$i<count($data);$i++){
    if($j < sqrt(count($data)) ){
        $data2[$j][$k] = $data[$i];
        $j++;
    }else{$j = 0; $k++;}
}

About matrix multiplication, I found this interesting post: http://sickel.net/blogg/?p=907 Using the exact function found there:

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);
}
$c = matrixmult($data2, $data2);
foreach($c as $k => $v){
    $i = 0;
    foreach($v as $kk => $vv){
        echo $vv . ' | ';
        $i++;
        if($i == count($v))
            echo '<br/>';
    }
}

The result is quite close to the needed pattern:

2.9999 | 1.6 | 8.5 | 
5.6665 | 3 | 16 | 
1.0666 | 0.56665 | 2.9999 |  

The slight difference comes from the rounding method. If that's an issue, see round() function.

sqrt() function is for calculating square root. If you want to loop all elements in array then remove sqrt() call.

In PHP, array usually zero-based index, so you need to replace $i=1, $j=1 to start from 0 in your for loop.

See Matrix multiplication

Matrix multiplication