1

I have this code, and it works. It just seems like there may be a better way to do this. Does anyone know a cleaner solution?

def Matrix2toMatrix(Matrix2):
    scaleSize = len(Matrix2[0, 0])

    FinalMatrix = np.empty([len(Matrix2)*scaleSize, len(Matrix2[0])*scaleSize])
    for x in range(0, len(Matrix2)):
        for y in range(0, len(Matrix2[0])):
            for xFinal in range(0, scaleSize):
                for yFinal in range(0, scaleSize):
                    FinalMatrix[(x*scaleSize)+xFinal, (y*scaleSize)+yFinal] = Matrix2[x, y][xFinal, yFinal]
    return FinalMatrix

This is where Matrix2 is a 4x4 matrix, with each cell containing a 2x2 matrix


Full code in case anyone was wondering:

import matplotlib.pyplot as plt
import numpy as np
    

def Matrix2toMatrix(Matrix2):
    scaleSize = len(Matrix2[0, 0])

    FinalMatrix = np.empty([len(Matrix2)*scaleSize, len(Matrix2[0])*scaleSize])
    for x in range(0, len(Matrix2)):
        for y in range(0, len(Matrix2[0])):
            for xFinal in range(0, scaleSize):
                for yFinal in range(0, scaleSize):
                    FinalMatrix[(x*scaleSize)+xFinal, (y*scaleSize)+yFinal] = Matrix2[x, y][xFinal, yFinal]
    return FinalMatrix


XSize = 4
Xtest = np.array([[255, 255, 255, 255]
                 ,[255, 255, 255, 255]
                 ,[127, 127, 127, 127]
                 ,[0, 0, 0, 0]
                  ])

scaleFactor = 2

XMarixOfMatrix = np.empty([XSize, XSize], dtype=object)
Xexpanded = np.empty([XSize*scaleFactor, XSize*scaleFactor], dtype=int)  # careful, will contain garbage data

for xOrg in range(0, XSize):
    for yOrg in range(0, XSize):

        newMatrix = np.empty([scaleFactor, scaleFactor], dtype=int)  # careful, will contain garbage data

        # grab org point equivalent
        pointValue = Xtest[xOrg, yOrg]

        newMatrix.fill(pointValue)

        # now write the data
        XMarixOfMatrix[xOrg, yOrg] = newMatrix


# need to concat all matrix together to form a larger singular matrix
Xexpanded = Matrix2toMatrix(XMarixOfMatrix)

img = plt.imshow(Xexpanded)
img.set_cmap('gray')
plt.axis('off')
plt.show()
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
  • Did the posted solution work for you? – Divakar Aug 28 '20 at 08:00
  • I actually get an error: m, n = Matrix2.shape[0], Matrix2.shape[2] --> IndexError: tuple index out of range. I am also totally lost by your method. Could you maybe explain it a bit, or point me to a resource? – Beachhouse Aug 28 '20 at 17:58
  • I assumed `Matrix2` as a 4D array. If it's not, you need to use `Matrix2 = np.array(Matrix2)` and then use posted codes. Can you test that out? Resource wise - https://stackoverflow.com/a/47978032/. – Divakar Aug 28 '20 at 18:06

1 Answers1

1

Permute axes and reshape -

m,n = Matrix2.shape[0], Matrix2.shape[2]
out = Matrix2.swapaxes(1,2).reshape(m*n,-1)

For permuting axes, we could also use np.transpose or np.rollaxis, as functionally all are the same.

Verify with sample run -

In [17]: Matrix2 = np.random.rand(3,3,3,3)

# With given solution
In [18]: out1 = Matrix2toMatrix(Matrix2)

In [19]: m,n = Matrix2.shape[0], Matrix2.shape[2]
    ...: out2 = Matrix2.swapaxes(1,2).reshape(m*n,-1)

In [20]: np.allclose(out1, out2)
Out[20]: True
Divakar
  • 218,885
  • 19
  • 262
  • 358