O
O
Olddd2021-04-17 10:00:14
Python
Olddd, 2021-04-17 10:00:14

How to fix numpy array rotation?

Hello! I wrote the code, it gives in rot90 raise ValueError("Axes must be different.")

import numpy as np


def super_sort(rows, cols):
    arr = np.array([np.random.randint(1, 100) for i in range(rows * cols)])
    arr.reshape(rows, cols)
    arr_b = np.rot90(np.copy(arr), k=1, axes=(0, 1))
    for i in range(len(arr_b)):
        if i % 2 == 0:
            arr_b = sorted(arr_b)
        else:
            arr_b = sorted(arr_b, reverse=True)
    arr_b = np.rot90(arr_b, k=3, axes=(0, 1))
    return arr, arr_b

The code is not complicated, I think comments are not required
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-04-17
@Vindicar

the reshape() method returns a version of the array with different dimensions, rather than changing those dimensions in place.
Since you're ignoring the return value, you're trying to rotate a one-dimensional vector.
But there will still be problems with sorted(arr_b), since sorted() interprets a two-dimensional array as a collection of one-dimensional ones, and tries to sort only this collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question