Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy concatenating multiple axes

I am not sure why I can not concatenate multiple dimensions of an array together using the the numpy.concatenate function. For example:

    array_2d.shape = [1200,1200]
    array2_2d.shape = [1200,1200]
    final_array1 = numpy.concatenate((array_2d,array2_2d),axes=0) # shape = (2400,1200)
    final_array2 = numpy.concatenate((array_2d,array2_2d),axes=1) # shape = (1200,2400)

Is there a way I can get the 2 arrays to concatenate both axes to yield a shape of (2400,2400)? Or am I just thinking of this approach incorrectly with the concatenation of arrays? Some help would be much appreciated!!!

like image 848
Jason Avatar asked Mar 15 '26 19:03

Jason


2 Answers

Let's think about what you're trying to accomplish, let's call array1 A, and array2 B, and some unknown array as X. Like you said, the following is 2400 x 1200:

 | A |
 | B |

But this would be a 2400 by 2400 array:

 | A | X |
 | X | B |

and so would this:

 | A | X |
 | B | X |

and this...:

 | A | A |
 | B | B |

The real question is how many times you want to concatenate each array and in which dimension or if you want to zero pad:

 | A | 0 |
 | 0 | B |

Which would be accomplished by using numpy to create an array of zeros and concatenating in the appropriate direction on both A and B before concatenating the results together.

like image 195
Brock Hargreaves Avatar answered Mar 18 '26 08:03

Brock Hargreaves


You should use numpy.tile(). Read this link for more information.

example:

a = np.array(([1,2],[3,4]))
b = np.tile(a, (2,2))

will create a 4x4 array from a 2x2 array, by repeating both axis.

like image 32
EhsanYaghoubi Avatar answered Mar 18 '26 09:03

EhsanYaghoubi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!