-1

I cannot understand why retrieved value of original Matrix isn't the good one. For instance, value of Matrix[0,3] should give 3 but gives 0 so that I am unable to get the correct 90° Matrix rotation:

import numpy as np
x = np.matrix(np.arange(16).reshape((4,4)))
print(x)

def rotate_90(matrix):
    rot_Mat = matrix
    len = matrix.shape[0]
    # len_range = range(len)
    # rev_range = reversed(len_range)
    for i in range(0, len):
        for j in range(0, len):

            value = matrix[i,j]
            print(i , j , value)
            
            rot_Mat[j,len - i -1] = value
    print(rot_Mat)

rotate_90(x)
Lyndon Gingerich
  • 604
  • 7
  • 17
Alan
  • 157
  • 8

1 Answers1

0
rot_Mat = matrix

This does not create a new array. rot_Mat is the same array as matrix, therefore you overwrite the values while reading them.

To create a new array with the same shape, use empty_like:

rot_Mat = np.empty_like(matrix)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65