0

I have a two numpy.arrays, I want to get following result efficiently

1.add the element's of b to a's sub-array

    a=numpy.array([(1,2,3),(1,2,3)])
    b=numpy.array([0,0])
->
    c=[(0,1,2,3),(0,1,2,3)] 

code in a loop

a=numpy.array([(1,2,3),(1,2,3)])
b=numpy.array([(0,0)])
c=numpy.zeros(2 , 4)
idx=0
for x in a:
   c[idx]=(a[idx][0],a[idx][1],a[idx][2], b[idx])
   idx = idx+1

and
2. Get an 2-D array with dimension(a.dim*b.dim, 2) from two 1-D arrays

    a=numpy.array([(1,2)])
    b=numpy.array([(3,4)])
->
    c=[(1,3),(1,4),(2,3),(2,4)]

code in a loop

a=numpy.array([(1,2)])
b=numpy.array([(3,4)])
c=numpy.zeros(a.size*b.size , 2)
idx=0
for x in a:
    for y in b:
        c[idx]=(x,y)
        idx = idx+1
5
  • Can you show some context? This is possible, and not that hard, but it may not be a good idea. Commented Aug 28, 2013 at 1:53
  • Okay, now it looks like you're asking two different questions. Can you give a precise definition of the result you're trying to achieve? Commented Aug 28, 2013 at 1:55
  • I want to these two method to get new array. And I don't want to make this in a loop . I this use loop to make is not efficient Commented Aug 28, 2013 at 1:56
  • The two things you're trying to achieve look like entirely different things. If you think they're the same, it's not at all clear what you want to do. Commented Aug 28, 2013 at 1:57
  • Could you write a loop that achieves the effect that you're looking for, so it's clearer what you want? Commented Aug 28, 2013 at 1:58

2 Answers 2

3

For the first problem, you can define b differently and use numpy.hstack:

a = numpy.array([(1,2,3),(1,2,3)])
b = numpy.array([[0],[0]])
numpy.hstack((b,a))

Regarding the second problem, I would probably use sza's answer and create the numpy array from that result, if necessary. That technique was suggested in an old Stack Overflow question.

Sign up to request clarification or add additional context in comments.

6 Comments

Because I have a big array a, and actually b has the same element. Can i don't alloc b the same size of a
I'm not sure I understand what you mean, but you can probably use numpy.empty_like, zeros_like or ones_like. Look into array.fill too.
Yes , That's what i mean. But I don't want to use ones_like, This will alloc a big memory. And all the elements of b are the same, So it's a little memory-wasting
I see what you mean now. sza's answer is better for that problem too, then, just use a fixed y.
But he also use the loop. I'm afraid it's not efficient. So I should make trade-off on it right?
|
2

For the first one, you can do

>>> a=numpy.array([(1,2,3),(1,2,3)])
>>> b=numpy.array([0,0])
>>> [tuple(numpy.insert(x, 0, y)) for (x,y) in zip(a,b)]
[(0, 1, 2, 3), (0, 1, 2, 3)]

For the 2nd one, you can get the 2-D array like this

>>> a=numpy.array([(1,2)])
>>> b=numpy.array([(3,4)])
>>> import itertools
>>> c = list(itertools.product(a.tolist()[0], b.tolist()[0]))
[(1, 3), (1, 4), (2, 3), (2, 4)]

1 Comment

This is list. can I also do this in numpy.array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.