Transpose an arbitrarily sized rectangular Matrix.




#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
'----------------------------------------------------------------------
SUB TransposeMatrix(InitMatrix() AS DWORD, TransposedMatrix() AS DWORD)
LOCAL l1, l2, u1, u2 AS LONG
  l1 = LBOUND(InitMatrix, 1)
  l2 = LBOUND(InitMatrix, 2)
  u1 = UBOUND(InitMatrix, 1)
  u2 = UBOUND(InitMatrix, 2)
  REDIM TransposedMatrix(l2 TO u2, l1 TO u1)
  MAT TransposedMatrix() = TRN(InitMatrix())
END SUB
'----------------------------------------------------------------------
SUB PrintMatrix(a() AS DWORD)
LOCAL l1, l2, u1, u2, r, c AS LONG
LOCAL s AS STRING * 8
  l1 = LBOUND(a(), 1)
  l2 = LBOUND(a(), 2)
  u1 = UBOUND(a(), 1)
  u2 = UBOUND(a(), 2)
  FOR r = l1 TO u1
    FOR c = l2 TO u2
      RSET s = STR$(a(r, c))
      CON.PRINT s;
    NEXT c
  CON.PRINT
  NEXT r
END SUB
'----------------------------------------------------------------------
SUB TranspositionDemo(BYVAL DimSize1 AS DWORD, BYVAL DimSize2 AS DWORD)
LOCAL r, c, cc AS DWORD
LOCAL a() AS DWORD
LOCAL b() AS DWORD
  cc = DimSize2
  DECR DimSize1
  DECR DimSize2
  REDIM a(0 TO DimSize1, 0 TO DimSize2)
  FOR r = 0 TO DimSize1
    FOR c = 0 TO DimSize2
      a(r, c)= (cc * r) + c + 1
    NEXT c
  NEXT r
  CON.PRINT "initial matrix:"
  PrintMatrix a()
  TransposeMatrix a(), b()
  CON.PRINT "transposed matrix:"
  PrintMatrix b()
END SUB
'----------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
  TranspositionDemo 3, 3
  TranspositionDemo 3, 7
END FUNCTION