rando
rando

Reputation: 377

concatenate numpy columns in different positions

I have an array x = np.empty([2,3]). Assume I have two set of logical indices indx1 and indx2 and each one of them is paired with different columns, set1 and set2:

 indx1 = [False,False,True]
 set1 = np.array([[-1],[-1]])
 indx2 = [True,True,False]
 set2 = np.array([[1,2],[1,2]])
 #need to join these two writing operations to a one.
 x[:,indx1] = set1
 x[:,indx2] = set2
 >>> x
array([[1., 2., -1.],
       [1., 2., -1.]])

How can I use indx1 and indx2 at the same time? For instance, I am looking for something like this (which does not work):

x[:,[indx1,indx2]] = [set1,set2]

Upvotes: 0

Views: 205

Answers (2)

Tawy
Tawy

Reputation: 629

I did not manage to find an exact solution to the problem, but maybe (depending on how you generate the sets and indices), this will lead you in the right direction.

Let's suppose that, instead of the sparse definition of set1 and set2, you have dense arrays, each with the same size as x:

indx1 = [False,False,True]
indx2 = [True,True,False]
fullset1 = np.array([[0, 0, -1],
                     [0, 0, -1]])
fullset2 = np.array([[1, 2, 0],
                     [1, 2, 0]])

x = np.select( [indx1, indx2], [fullset1, fullset2] )
print(x)
#[[1 2 -1]
# [1 2 -1]]

It works with one command and can be easily extended if you have indx3, indx4, etc. However, I see several drawbacks. First, it creates a new variable that satisfies the conditions, which may not be your use case. Also, if there is an index that is set to false for all indx variables, the result might be unexpected:

indx1 = [False,False,True,False]
indx2 = [True,True,False,False]
fullset1 = np.array([[0, 0, -1, 0],
                     [0, 0, -1, 0]])
fullset2 = np.array([[1, 2, 0, 0],
                     [1, 2, 0, 0]])

x = np.select( [indx1, indx2], [fullset1, fullset2], default=None )
print(x)
#[[1 2 -1 None]
# [1 2 -1 None]]

In that case, my proposal (but I haven't tested the performances) would be to use an intermediate variable and np.where to fill the final variable:

x = np.array([[11, 12, 13, 14], 
              [15, 16, 17, 18]])
#....
intermediate_x = np.select( [indx1, indx2], [fullset1, fullset2], default=None )

indx_final = np.where(intermediate_x == None)
x[indx_final] = intermediate_x[indx_final]
print(x)
#[[ 1  2 -1 14]
# [ 1  2 -1 18]]

Upvotes: 1

Piotr Żak
Piotr Żak

Reputation: 2132

In your case there are array, which have different dimensions (axis=0 if there the same dimension, and axis=1 if there is different dimensions)

For the easiest concatenate:

import numpy as np

set1 = np.array([[3],[3]])
set2 = np.array([[1,2],[1,2]])

indx1 = [False,False,True]
indx2 = [True,True,False]

sets = np.concatenate((set1, set2), axis=1)
np.concatenate((indx1, indx2), axis=0)

sets.sort()

output sets:

enter image description here

output index:

enter image description here


If you wan't to correlate sets with index - provide the proper output.

Upvotes: 1

Related Questions