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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)