HighwayJohn
HighwayJohn

Reputation: 921

Compare matrices in python and copy values

I have the following question and need help. Let me give you an example to describe my problem: I have two matrices

wp=[[0, 0, 0, 0]    m_data=[[x0, 0, 0, 0] 
    [0, 0, 0, 1]            [x1, 0, 1, 0]
    [0, 0, 1, 0]            [x2, 0, 0, 1]
    [0, 0, 1, 1]            [x3, 1, 1, 1]
    [0, 1, 0, 0]            [x4, 1, 0, 1]
    [0, 1, 0, 1]            [x5, 1, 1, 0]
    [0, 1, 1, 0]            [x6, 0, 1, 1] 
    [0, 1, 1, 1]]           [x7, 1, 0, 0]]

I get the second matrix by reading a .csv file

my_data = numpy.genfromtxt('Untitled 1.csv', delimiter=','
my_data = numpy.matrix(my_data))

The first matrix will be created by

wp = (numpy.arange(2**l)[:,None] >> numpy.arange(l)[::-1]) & 1
wp = numpy.hstack([wp.sum(1,keepdims=True), wp])
wp = numpy.c_[numpy.zeros(a**l), wp]
wp = wp[wp[:,2].argsort()]

The size of the matrix varies but in my current problem I have 8 spaces for 0 & 1, thus a size of 256*9. In the example I have 3 spaces for 0 & 1, thus the matrix have the dimension 8*4. Now I want to copy the x values in the first column of the second matrix to first column of the first matrix. But I want to copy it of course in the correct row. Do you know an easy fix? I would be very happy.

Upvotes: 0

Views: 81

Answers (1)

paddyg
paddyg

Reputation: 2245

Following Forzaa's suggestion you might be able to do something like

ix = np.lexsort((my_data[:,1], my_data[:,2], my_data[:,3]))
wp[:,0] = my_data[ix,0]

EDIT: P.S. I might have misunderstood your system to generate wp, but it looks very complicated. Could you do something like

wp = np.zeros((2**L, L+1))
wp[:,1:] = [[(i >> j) & 1 for j in range(L-1,-1,-1)] for i in range(2**L)]

Upvotes: 1

Related Questions