progrez.cloud

MATRIXXX GESER

08 Juli 2020

Ini cerita tentang aku dan asbak di atas mejaku, mengembara di antara puing-puing biner untuk menulis sejarah hidupku. Ketika mengembara untuk mencurahkan krikil hidup, aku di pahami akan kesederhanaan.


Ada beberapa kasus, yang harus di pahami manusia dalam hidup ini, ya itu menggeser nilai-nilai norma untuk mencerna kebenaran. Contohnya menggeser matriks pada gambar di bawah ini :



Saya di challenge untuk merealisasikan algoritma yang ada pada foto di atas, dengan waktu tak terhingga.


Me : Gampang bang -_-

Abg : Ayok, bikin ..

Me : 2 menit udah jadi :)

Abg : Masa sih, aku 1/2 jam baru bisa, keknya logic ku makin turun deh

Me : Iyo, bang ..


Di mulai dari menyalakan apache, dan menyeruput susu, di sertai sebutir panadol, pada 2:34 siang itu semua, bermula.


<?php

   $A = [
     [1 ,2 ,3 ,4 ,5 ],
     [6 ,7 ,8 ,9 ,10],
     [11,12,13,14,15],
     [16,17,18,19,20],
     [21,22,23,24,25]
   ];

   print_r(matriksGeser($A, 3));

?>


Setelah selesai dengan meng inisialisasi, bentukan. Akupun mulai membuat cakaran-cakaran untuk nanti di tirukan oleh fungsi ku, di mana :

matriksGeser(Himpunan, potongan)


Di ketahui
b = baris
k = kolom
$bM = baris matrix
$kM = kolom matrix

Example 1
[0] = 1     ,2     ,3     ,6     ,7     ,8     ,11    ,12    ,13
[0] = [0][0],[0][1],[0][2],[1][0],[1][1],[1][2],[2][0],[2][1],[2][2]

[b][k]
--------------------
[0][0],[0][1],[0][2],
[1][0],[1][1],[1][2],
[2][0],[2][1],[2][2]

Example 2
[0] = 2     ,3     ,4     ,7     ,8     ,9     ,12    ,13    ,14
[0] = [0][1],[0][2],[0][3],[1][1],[1][2],[1][3],[2][1],[2][2],[2][3]

[b][k]
--------------------
[0][1],[0][2],[0][3],
[1][1],[1][2],[1][3],
[2][1],[2][2],[2][3]

Geser (B:vertikal, K:horizontal)
B | K
--------------------
0 | 0 1 2
1 | 0 1 2
2 | 0 1 2

0 | 1 2 3
1 | 1 2 3
2 | 1 2 3

0 | 2 3 4
1 | 2 3 4
2 | 2 3 4

1 | 0 1 2
2 | 0 1 2
3 | 0 1 2

dst ...

Finally
[0] = 1,2,3,6,7,8,11,12,13
[1] = 2,3,4,7,8,9,12,13,14
[2] = 3,4,5,8,9,10,13,14,15
 */

?>


Tak terasa waktu udah menjelang senja, dan kegelapan perlahan mulai menyelimuti langit di mataku. Namun tepat jam 10:32 WIT, aku berhasil membuat, algoritma yang ku beri nama Matriks Geser, dengan 4 perulangan tepatnya.


<?php

function matriksGeser($data, $width){
   
  $out = null;

  //geser matriks

  for ($bM=0; $bM < $width; $bM++) {
     $loopOut = array();
     for ($kM=0; $kM <$width ; $kM++) {
         for ($b=$bM; $b <= count($data)-($width-$bM) ; $b++) {
             $newOut = array();
             //echo "<br>".$b." | ";
             for ($k=$kM; $k <= count($data[$b])-($width-$kM) ; $k++) {
                 //echo " ".$k;
                 $newOut[] = $data[$b][$k];
             }
             $loopOut[$b] = $newOut;
         }
         $out[$bM][$kM] = $loopOut;
      }        
 }
 return $out;  
}

?>


Dan, sampai di sini dulu kisah, tak berarti ini. Semoga tidak bermanfaat. Btw, rencananya, mau bikin video pemanfaatan fungsi ini, tapi nanti liat deh.


Finally..

(
   [0] => Array ( 
      [0] => Array ( 
         [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) 
         [1] => Array ( [0] => 6 [1] => 7 [2] => 8 ) 
         [2] => Array ( [0] => 11 [1] => 12 [2] => 13 ) 
      ) 
      [1] => Array ( 
         [0] => Array ( [0] => 2 [1] => 3 [2] => 4 ) 
         [1] => Array ( [0] => 7 [1] => 8 [2] => 9 ) 
         [2] => Array ( [0] => 12 [1] => 13 [2] => 14 ) 
      ) 
      [2] => Array ( 
         [0] => Array ( [0] => 3 [1] => 4 [2] => 5 ) 
         [1] => Array ( [0] => 8 [1] => 9 [2] => 10 ) 
         [2] => Array ( [0] => 13 [1] => 14 [2] => 15 ) 
      ) 
   ) 
   [1] => Array ( 
      [0] => Array ( 
         [1] => Array ( [0] => 6 [1] => 7 [2] => 8 ) 
         [2] => Array ( [0] => 11 [1] => 12 [2] => 13 ) 
         [3] => Array ( [0] => 16 [1] => 17 [2] => 18 ) 
      ) 
      [1] => Array ( 
         [1] => Array ( [0] => 7 [1] => 8 [2] => 9 ) 
         [2] => Array ( [0] => 12 [1] => 13 [2] => 14 ) 
         [3] => Array ( [0] => 17 [1] => 18 [2] => 19 ) 
      ) 
      [2] => Array ( 
         [1] => Array ( [0] => 8 [1] => 9 [2] => 10 ) 
         [2] => Array ( [0] => 13 [1] => 14 [2] => 15 ) 
         [3] => Array ( [0] => 18 [1] => 19 [2] => 20 ) 
      ) 
   ) 
   [2] => Array ( 
      [0] => Array ( 
         [2] => Array ( [0] => 11 [1] => 12 [2] => 13 ) 
         [3] => Array ( [0] => 16 [1] => 17 [2] => 18 ) 
         [4] => Array ( [0] => 21 [1] => 22 [2] => 23 ) 
      ) 
      [1] => Array ( 
         [2] => Array ( [0] => 12 [1] => 13 [2] => 14 ) 
         [3] => Array ( [0] => 17 [1] => 18 [2] => 19 ) 
         [4] => Array ( [0] => 22 [1] => 23 [2] => 24 ) 
      ) 
      [2] => Array ( 
         [2] => Array ( [0] => 13 [1] => 14 [2] => 15 ) 
         [3] => Array ( [0] => 18 [1] => 19 [2] => 20 ) 
         [4] => Array ( [0] => 23 [1] => 24 [2] => 25 ) 
      ) 
   ) 
)