Matrix rotation

How can I rotate a submatrix? starting row and starting column is given also the size of submatrix is given. For example, I have a 5*5 matrix and x=1 starting row and y=2 starting col and the size of matrix is 3.

Hey @ritika_dhamija
You can rotate a submatrix inside a matrix the same way as you rotate a matrix. Just pass the use the start coordinates as the given coordinates and the end coordinates as X start coordinate+given size and Y start coordinate +given size

For rotating a matrix, you may follow two steps:
Find transpose of matrix.
Reverse columns of the transpose.

It does not work that way. Consider a 5*5 matrix and you want to rotate sub-matrix starting from (1,2) and of size 3. And this work doesn’t seem right there.

@ritika_dhamija
To transpose a submatrix, take the coordinates about (0, 0) i.e. shift them using i = i-rowstart, j = j-colstart, then find the coordinates after transposing about (0, 0) which will be i = j-colstart, j = i-rowstart, now find the actual coordinates you want to swap with, that is shift again, i = j-colstart+rowstart and j = i-rowstart+colstart. Do this for all the elements of the submatrix.

For reversing the columns, you’ve to swap the elements of sumatrix like:
for(int i = rowstart; i < (rowstart+size)/2; i++){
for(int j = colstart; j < colstart+size; j++){
swap(mat[i][j], mat[rowstart+size-1-i][j];
}
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.