Create a matrix from a vector where each row is a shifted version of the vector
问题 I have a numpy array like this import numpy as np ar = np.array([1, 2, 3, 4]) and I want to create an array that looks like this: array([[4, 1, 2, 3], [3, 4, 1, 2], [2, 3, 4, 1], [1, 2, 3, 4]]) Thereby, each row corresponds to ar which is shifted by the row index + 1. A straightforward implementation could look like this: ar_roll = np.tile(ar, ar.shape[0]).reshape(ar.shape[0], ar.shape[0]) for indi, ri in enumerate(ar_roll): ar_roll[indi, :] = np.roll(ri, indi + 1) which gives me the desired