Jan-01-2023, 06:31 PM
Hi
Does somebody have a more efficient way to reshape the following M matrix?
The only way i've found so far is to extract 2 intermediate matrixes before concatenating them, but I'm persuaded one can do it in a more elegant way.
Thanks for any advice
Paul
Does somebody have a more efficient way to reshape the following M matrix?
The only way i've found so far is to extract 2 intermediate matrixes before concatenating them, but I'm persuaded one can do it in a more elegant way.
Thanks for any advice
Paul
import numpy as np
M = np.array([[0, 0, -1], # P1
[0, 0, -1],
[0, 0, 0],
[0, 0, 0], # P2
[0, 0, 1],
[0, 0, 1]])
r, c = np.shape(M)
k = 2
Target = np.array([[0, 0, -1, 0, 0, 0, 0, 0, 1],
[0, 0, -1, 0, 0, 0, 0, 0, 1]])
# M1 = M.reshape(k, c*(r//k))
# Test1 = np.array_equal(Target, M1)
# M2 = M.reshape(k, c*(r//k), order='C')
# Test2 = np.array_equal(Target, M2)
# M3 = M.reshape(k, c*(r//k), order='F')
# Test3 = np.array_equal(Target, M3)
# M4 = M.reshape(k, c*(r//k), order='A')
# Test4 = np.array_equal(Target, M4)
i = np.arange(0, r, k)
j = np.arange(1, r, k)
M5 = np.vstack((M[i, :].reshape(1, (r*c//k)), M[j, :].reshape(1, (r*c//k))))
Test5 = np.array_equal(Target, M5)
print(f"M5 = {M5}")
print(f"Test5 = {Test5}")
