donglv7097 2016-11-02 04:19
浏览 32
已采纳

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 :'(

  • 写回答

2条回答 默认 最新

  • duanchen7703 2016-11-02 09:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费