Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 output.

My question is whether there is a smarter way of doing this which avoids the loop.

like image 961
Cleb Avatar asked May 02 '17 09:05

Cleb


People also ask

How do you switch rows and columns of a matrix in Python?

To transpose NumPy array ndarray (swap rows and columns), use the T attribute ( . T ), the ndarray method transpose() and the numpy. transpose() function.

How do you generate a random matrix in Python?

numpy. random. rand(row, column) generates random numbers between 0 and 1, according to the specified (m,n) parameters given. So use it to create a (m,n) matrix and multiply the matrix for the range limit and sum it with the high limit.


1 Answers

Here's one approach using NumPy strides basically padding with the leftover elements and then the strides helping us in creating that shifted version pretty efficiently -

def strided_method(ar):
    a = np.concatenate(( ar, ar[:-1] ))
    L = len(ar)
    n = a.strides[0]
    return np.lib.stride_tricks.as_strided(a[L-1:], (L,L), (-n,n))

Sample runs -

In [42]: ar = np.array([1, 2, 3, 4])

In [43]: strided_method(ar)
Out[43]: 
array([[4, 1, 2, 3],
       [3, 4, 1, 2],
       [2, 3, 4, 1],
       [1, 2, 3, 4]])

In [44]: ar = np.array([4,9,3,6,1,2])

In [45]: strided_method(ar)
Out[45]: 
array([[2, 4, 9, 3, 6, 1],
       [1, 2, 4, 9, 3, 6],
       [6, 1, 2, 4, 9, 3],
       [3, 6, 1, 2, 4, 9],
       [9, 3, 6, 1, 2, 4],
       [4, 9, 3, 6, 1, 2]])

Runtime test -

In [5]: a = np.random.randint(0,9,(1000))

# @Eric's soln
In [6]: %timeit roll_matrix(a)
100 loops, best of 3: 3.39 ms per loop

# @Warren Weckesser's soln
In [8]: %timeit circulant(a[::-1])
100 loops, best of 3: 2.03 ms per loop

# Strides method
In [18]: %timeit strided_method(a)
100000 loops, best of 3: 6.7 µs per loop

Making a copy (if you want to make changes and not just use as a read only array) won't hurt us too badly for the strides method -

In [19]: %timeit strided_method(a).copy()
1000 loops, best of 3: 381 µs per loop
like image 108
Divakar Avatar answered Nov 15 '22 00:11

Divakar