Rotating a matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • llorenzi
    New Member
    • Apr 2010
    • 1

    Rotating a matrix

    Hi everybody.
    I have a code to modify. This is the original code:
    Code:
     
    N=nn-n+1;  M=mm-m+1;
      for (j=1; j<=N; ++j) {
        J=j+n-1;
        for (i=1; i<=M; ++i) {
          I=i+m-1;
          
          for (jj=j,jj2=1; jj<=J; ++jj,++jj2) {
    		for (ii=i,ii2=1; ii<=I; ++ii,++ii2) {
    			ndx=ii-1+mm*(jj-1);
    			ndx2=ii2-1+m*(jj2-1);
    			if (!toFill[ndx2]) {
    				err=img[ndx      ] - Ip[ndx2    ]; patchErr += err*err;
    				err=img[ndx+=mmnn] - Ip[ndx2+=mn]; patchErr += err*err;
    				err=img[ndx+=mmnn] - Ip[ndx2+=mn]; patchErr += err*err;
    			  }
    			}
    		  }
    
          }
      }
    I have to rotate the little matrix (img), so it does mean to rotate his index: ndx

    For example if I have to flip it, I just write:
    Code:
     
    ndx=(I-ii+1)-1+mm*(jj-1);
    But if I want to rotate it (+90'), how can I do?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Write a 3x3 matrix on paper and rotate it 90 degrees. The bottom row becomes the first column. Develop your algorithm from what you had to do with your pencil.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Note that rotate 90 degrees could also be:
      flip +
      diagonal invert (eg x = y, y = x)

      The direction of flipping depends on which way you're rotating.

      Comment

      Working...