Yedhu Krishnan
Yedhu Krishnan

Reputation: 1245

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?

Upvotes: 2

Views: 187

Answers (3)

B. M.
B. M.

Reputation: 18658

An other writable solution :

def block(X):
   n=X.size
   zeros=np.zeros((2*n-1,n),X.dtype)
   zeros[::2]=X
   return zeros.reshape(n,-1).T

try :

In [2]: %timeit block(X)
600 µs ± 33 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Upvotes: 1

Divakar
Divakar

Reputation: 221614

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 create a read-only output. If you need to write into later on, simply make a copy by appending .copy() at the end.

Approach #2 : Using concatenation with zeros and then clipping/slicing -

def zeropad_arr_v2(X):
    n = len(X)
    X_ext = np.concatenate((X, np.zeros(n,dtype=X.dtype)))
    return np.tile(X_ext, n)[:-n].reshape(-1,n,order='F')

Approach #1 being a strides-based method should be very efficient on performance.

Sample runs -

In [559]: X = np.array([1,2,3])

In [560]: zeropad_arr_v1(X)
Out[560]: 
array([[1, 0, 0],
       [2, 1, 0],
       [3, 2, 1],
       [0, 3, 2],
       [0, 0, 3]])

In [561]: zeropad_arr_v2(X)
Out[561]: 
array([[1, 0, 0],
       [2, 1, 0],
       [3, 2, 1],
       [0, 3, 2],
       [0, 0, 3]])

Runtime test

In [611]: X = np.random.randint(0,9,(1000))

# Approach #1 (read-only)
In [612]: %timeit zeropad_arr_v1(X)
100000 loops, best of 3: 8.74 µs per loop

# Approach #1 (writable)
In [613]: %timeit zeropad_arr_v1(X).copy()
1000 loops, best of 3: 1.05 ms per loop

# Approach #2
In [614]: %timeit zeropad_arr_v2(X)
1000 loops, best of 3: 705 µs per loop

# @user8153's solution
In [615]: %timeit hstack_app(X)
100 loops, best of 3: 2.26 ms per loop

Upvotes: 1

user8153
user8153

Reputation: 4095

If you read the desired output matrix top-down, then left-right, you see the pattern 1,2,3, 0,0,0, 1,2,3, 0,0,0, 1,2,3. You can use that pattern to easily create a linear array, and then reshape it into the two-dimensional form:

import numpy as np
X = np.array([1,2,3])
N = len(X)
zeros = np.zeros_like(X)
m = np.hstack((np.tile(np.hstack((X,zeros)),N-1),X)).reshape(N,-1).T
print m

gives

[[1 0 0]
 [2 1 0]
 [3 2 1]
 [0 3 2]
 [0 0 3]]

Upvotes: 2

Related Questions