This is a continuation from here.
If I have an array a and an array b and a can be smaller or larger than b.Is there a way , having as base array the a, to replace all elements of b (element by element) with elements of a?
For example, this works because a has as many elements as b:
import numpy as np
a = np.array([1,2,3,4,5,6,7,8])
b = np.array([[1,2,3,4],[11,22,33,44]])
print(b[0].shape)
i= 0
for el in b:
for idx,x in enumerate(el):
el[idx] = a[i]
i+= 1
print(b)
[[1 2 3 4]
[5 6 7 8]]
Now, for example, a can be:
a = np.array([1,2,3,4,5])
and b:
b = np.array([11,22,33,44])
The result I want is b = [1,2,3,4,5] because all the 4 elements of b are replaced by the first 4 elements of a and I add one more element to b (the last of a) in order to be the same size as a(my reference array).
If I have :
a = np.array([1,2,3]) and b = np.array([6,7,8,9]) , then the result b array will be : b = [1,2,3] , so I deleted one element from b (to be the same size as a) and replaced the rest with the a elements.
Now, the whole problem is that:
I will have an array which has number of elements multiples of a size.
So , a = np.array([1,2,3,4,5])
arr = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.array([[0,1,2,3], [4,5,6,7]])
I want to replace the elements from arr to b in multiples of a size (hence 5).
So , final b = [ [1,2,3,4,5], [6,7,8,9,10])
arrandahere. Can you tell what isarrin your first example? Is ita=np.array([1])andarr=np.array([1,2,3,4,5])?arrwas just the elements of b( I changed the name toelnow)if you mean that.Generally, I want to replace allbelements byarrvalues by multiples ofasize (so by every 5 elements in my example)ato add a row? Only whenb.size // a.size > 1, b.size % a.size == 0?a.size % b.shape[1] /= 0?