title: Is there a function which keeps increasing a number in a matrix in given order
body: <p>I have to create a matrix (N*M) where each cell is labeled in  according to their order with specific traversal. </p>
  
  <p>The problem is that I have it only working for (N<em>N). I am stuck to find a solution so it can also work for (N</em>M).</p>
  
  <p>For instance 
  Example with N*M: </p>
  
  <pre><code>1  2  5  10
  
  4  3  6  11
  
  9  8  7  12
  </code></pre>
  
  <p>Example with N*N: </p>
  
  <pre><code>1  2  5  10 
  
  4  3  6  11 
  
  9  8  7  12 
  
  16 15 14 13
  </code></pre>
  
  <p>2x4 will look like:</p>
  
  <pre><code>1 2 5 7
  
  4 3 6 8
  </code></pre>
  
  <p>4x2 will look like:</p>
  
  <pre><code>1 2
  
  4 3
  
  5 6
  
  7 8
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>public static int matrix[][];
         public static void main(String[] args) {
  
          borderLayout(5);
          print();
         }
  
         public static void borderLayout(int size) {
             matrix = new int[size][size];
             for(int i = 0 ; i &lt; size ; i ++) {
                 fillBorder(i);
             }
         }
  
         public static void fillBorder(int n) {
             int number = n*n+1;
             int row = n;
             int column = 0;
  
  
             for(column = 0; column&lt;=n; column++) {
                 System.out.println("COLUMN DRAWING");
                 matrix[row][column]=number++;
             }
  
             column--;
  
             for(row = n; row&gt;0 ; row--){
                 System.out.println("ROW DRAWING");
                 matrix[row-1][column] = number++;
             }
  
             print();
             System.out.println("");
  
         }
  
         private static void print() {
  
                int x = matrix.length;
                int y = matrix[0].length;
  
                for (int i = 0; i &lt; x; i++) {
  
                   for (int j = 0; j &lt; y; j++) {
  
                      System.out.print(matrix[j][i]);
                      System.out.print(" ");
                      if (matrix[j][i] &lt; 10) {
                         System.out.print(" ");
                      }
                   }
  
                   System.out.println();
                }
  
             }
  </code></pre>