What is the best way to create a block matrix form a row vector?
问题 I have the following numpy row matrix. X = np.array([1,2,3]) I want to create a block matrix as follows: 1 0 0 2 1 0 3 2 1 0 3 2 0 0 3 How can I do this using numpy? 回答1: Approach #1 : Using np.lib.stride_tricks.as_strided - from numpy.lib.stride_tricks import as_strided as strided def zeropad_arr_v1(X): n = len(X) z = np.zeros(len(X)-1,dtype=X.dtype) X_ext = np.concatenate(( z, X, z)) s = X_ext.strides[0] return strided(X_ext[n-1:], (2*n-1,n), (s,-s), writeable=False) Note that this would