T
T
Timebird2016-05-19 16:23:12
Python
Timebird, 2016-05-19 16:23:12

How to get a matrix from arrays?

Hello! The problem is this.
I have 2 arrays as input. It is necessary to get the (i-1)-th element for each element i of the first array (and then the second one). That is, so that the element i-1 should be in place i, the element i should be in place i+1, i +1 should be in place i+2, and so on. And the same with the second array. And then push these two arrays into the np.matrix matrix (the first array on the first line, the second array on the second). How is it rational to do this and why doesn't it work like that?

list1 = []
 
    for i in range(len(array1)):
        a1         = array1[i-1]
        list1.append(a1)
    array1 = np.array(list1)
 
list2 = []
 
    for i in range(len(array2)):
        a2 = array2[i-1]
        list2.append(a2)
    array2 = np.array(list2)
 
    
total_array = np.vstack((array1, array2))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2016-05-19
@Timebird

def shiftList(lst, n):
    n = n % len(lst)
    return lst[n:] + lst[:n]

a = [1, 2, 3, 4, 5, 6, 7]
shiftList(a,1)

Well, since you are using numpy, then
import numpy
a = [1, 2, 3, 4, 5, 6, 7]
numpy.roll(a,1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question