0

I have an array :

a = np.array([1,2,3,4,5,6,7,8])

The array may be reshaped to a = np.array([[1,2,3,4],[5,6,7,8]]), whatever is more convenient.

Now, I have an array :

b = np.array([[11,22,33,44], [55,66,77,88]])

I want to replace to each of these elements the corresponding elements from a.

The a array will always hold as many elements as b has.

So, array b will be :

[1,2,3,4], [5,6,7,8]

Note, that I must keep each b subarray dimension to (4,). I don't want to change it.So, the idx will take values from 0 to 3.I want to make a fit to every four values.

I am struggling with reshape, split,mask ..etc and I can't figure a way to do it.

import numpy as np

#a = np.array([[1,2,3,4],[5,6,7,8]])
a = np.array([1,2,3,4,5,6,7,8])


b = np.array([[11,22,33,44], [55,66,77,88]])

for arr in b:
    for idx, x in enumerate(arr):
        replace every arr[idx] with corresponding a value
4
  • Can't you just use d as a replacement for a? Commented Feb 15, 2017 at 10:01
  • @languitar:No, as I said, I will have a loop which goes down until it reaches shape (4,) and there I want to do the replacement. Commented Feb 15, 2017 at 10:03
  • "I want to replace to each of these arrays the corresponding elements from a." this is an unclear statement. How? What do you mean by "corresponding"? Is it always true len(b)==len(c), len(a)==2*len(b)? Commented Feb 15, 2017 at 10:04
  • @DanielForsman:That's correct.a will have twice the elements of b.And c will be always equal to b. Commented Feb 15, 2017 at 10:06

4 Answers 4

2

For your current case, what you want is probably:

b, c = list(a.reshape(2, -1))

This isn't the cleanest, but it is a one-liner. Turn your 1D array into a 2D array with with the first dimension as 2 with reshape(2, -1), then list splits it along the first dimension so you can directly assign them to b, c

You can also do it with the specialty function numpy.split

b, c = np.split(a, 2)

EDIT: Based on accepted solution, vectorized way to do this is

b = a.reshape(b.shape)

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

4 Comments

Nice,thanks,but what can I do in this situation.(I edited my post)
Thanks for this solution.(upvoting)
Can't see your pastebin, but if you always want to split in half you'd want b=a.reshape(2,-1). if you always want 4 elements (and len(a) is always divisible by 4) you can do b=a.reshape(-1,4)
Thank you very much for the info!
1

The following worked for me:

i = 0
for arr in b:
    for idx, x in enumerate(arr):
        arr[idx] = a[i]
        print(arr[idx])
        i += 1

Output (arr[idx]): 1 2 3 4 5 6 7 8 If you type print(b) it'll output [[1 2 3 4] [5 6 7 8]]

Comments

0

b = a[:len(a)//2] c = a[len(a)//2:]

Comments

0

Well, I'm quite new to Python but this worked for me:

    for i in range (0, len(a)//2):
        b[i] = a[i]
    for i in range (len(a)//2,len(a)):
        c[i-4] = a[i]

by printing the 3 arrays I have the following output:

[1 2 3 4 5 6 7 8]
[1 2 3 4]
[5 6 7 8]

But I would go for Daniel's solution (the split one): 1 liner, using numpy API, ...

2 Comments

Hi, I edited my post.The thing is that I must replace at the same time only b array (edited in post)
I added another answer for more clarity ;)

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.