Rotating the two dimesional matrix in clock wise?

Will someone explain this process in step by step?how many times the for loop runs?

var a =  [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
   function rotateClockwise(a) {
        var n=a.length;
        for (var i=0; i<n/2; i++) {
            for (var j=i; j<n-i-1; j++) {
                var tmp=a[i][j];
                a[i][j]=a[n-j-1][i];
                a[n-j-1][i]=a[n-i-1][n-j-1];
                a[n-i-1][n-j-1]=a[j][n-i-1];
                a[j][n-i-1]=tmp;
            }
        }
        return a;
    }

//Output(a) =
//    [[7, 4, 1],
//     [8, 5, 2],
//     [9, 6, 3]]

What it does is tug 4 points at a time around into their correct positions.
Not exactly the easiest algorithm to look at, and somewhat problematic with non-square matricies
.
Better one would probably be to take the size of the dimensions of the array - X and Y, and the concatenated elements of the array in a single array, and then take the Y’th elements of the array as an array element, incrementing the offset until X. (This is how things like numpy determine their matricies.)

How many times does the loop run? You’ve got a simple example there, You tell me. Trace the loops. N has a fixed number (3), so…

  1. How many times does the outer loop run?
  2. For each value of i, how many times does the inner loop run?
  3. What is the sum of those numbers in 2) ?

its reallly giving me headache…

So answer question 1. That should be simple enough.
If N is 3, how many times does the outer loop run?

2times

Right. So, we know:
N is 3.
the outer loop will run 2 times.
the outer loop: for (var i=0; i<n/2; i++) {
So i starts as 0, and will become 1 (i++).
So.
if N is 3, and i is 0, how many times does the inner loop run?
if N is 3, and i is 1, how many times does the inner loop run?

2 and 0

And 2+0 = 2. So the innermost code will run 2 times.
If you look at the matrix, this makes sense; moving 4 things at a time, you can rotate the entire matrix by rotating the corners, then the middles of the sides.

Now try it with N=4. and N=5. And you might see the pattern in the total.

1 Like

ok thanks if i stuck i will come back…

And for those that stumble into this thread looking for it, my version works on the following theory:

For a given Matrix NxM: (Note: This works on non-square arrays!)
Example:
N=5,M=3
[[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
Or:
[
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
[13,14,15]
]

A clockwise shift represents rotation such that the elements of a given column X should at the end be the elements of row X, with the order of elements being reversed.
IE, the rotation of the above matrix should be the 3x5:
[
[13,10,7,4,1],
[14,11,8,5,2],
[15,12,9,6,3]
]

It is observed that if the elements of the matrix are laid out as a single dimensional array in ascending order across the rows of the matrix:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
that the elements corresponding to a given row of the resultant matrix must be kM distance from the other elements of that row, with k being an integer;
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
(The distance between any two numbers with the same coloring is M)

in this way, the resulting matrix can be formed by taking groupings of values with indices kM+o, with o being from 0 to N-1, and k being from 0 to array.length/M. These groupings must be reversed to provide the correct ordering of elements.

So,

function rotateMatrix(matrix) {
  var theta = matrix.reduce((omega,alpha) => omega.concat(alpha));
  var delta = [];
  for(x = 0; x < matrix[0].length; x++) {
      i = x;
     delta[x] = [];
      while(i < theta.length) {     
        delta[x].push(theta[i]);
        i += matrix[0].length;
      }
      delta[x].reverse();
   }
return delta;
}  

(to rotate counterclockwise, run the X loop in reverse, set i to matrix[0].length-1-x instead of x, and do not reverse the results.)

1 Like

Wow its amazing and easy to understand.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.