I have a numpy Ndarray of dimensions (N * N * M) and want to mirror it over the main diagonal efficiently. For N=1 I did the following:
A = np.array([[1, 0, 6, 5], [0, 2, 0, 0], [1, 0, 2, 0], [0, 1, 0, 3]])
A = np.tril(A) + np.triu(A.T, 1)
'''
From:
array([[1, 0, 6, 5],
[0, 2, 0, 0],
[1, 0, 2, 0],
[0, 1, 0, 3]])
To:
array([[1, 0, 1, 0],
[0, 2, 0, 1],
[1, 0, 2, 0],
[0, 1, 0, 3]])
'''
However this (np.tril and np.triu) doesn’t work for higher dimensions e.g.
A = np.array([[[1], [0], [6], [5]], [[0], [2],[0], [0]], [[1], [0], [2], [0]], [[0], [1], [0], [3]]]) # (4,4,1)
A = np.array([[[1,2], [0,3], [6,5], [5,6]], [[0,3], [2,2],[0,1], [0,3]], [[1,5], [0,2], [2,1], [0,9]], [[0,1], [1,2], [0,2], [3,4]]]) # (4,4,2)
Any ideas to do this efficiently (without for loops)? I don’t mind if you mirror the bottom or the top triangle of the matrix