Post Reply 
Matrix Rotation of elements - Algorithms
12-15-2019, 06:16 AM (This post was last modified: 12-16-2019 12:54 PM by Ángel Martin.)
Post: #4
RE: Matrix Rotation of elements - Algorithms
here's a start, surely not the best approach but it seems to do the job:


for an (m x n) matrix, the rotated matrix (one single step, clockwise) bij is given as follows:-

FOR k = 0 TO int(m/2)-1 ; current layer

FOR j = k+1 TO (n-1-k) ; top row rightwards
b(1+k),(j+1) = a(1+k),j
NEXT j

FOR i = (1+k) TO (m-1-k) ; rightmost column downwards
b(i+1),(n-k) = a i,(n-k)
NEXT i

FOR j = (n-k) TO (2+k) STEP -1 ; bottom row leftwards
b(m-k),(j-1) = a(m-k),j
NEXT j

FOR i = (m-k) TO (2+k) STEP -1 ; leftfmost column upwards
b(i-1),(1+k) = a i,(1+k)
NEXT i

NEXT k ; next layer

Edited: corrected typos in indexes

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Matrix Rotation of elements - Algorithms - Ángel Martin - 12-15-2019 06:16 AM



User(s) browsing this thread: 1 Guest(s)