简体   繁体   中英

numpy array construction with broadcasting

I would like stack together arrays that have different, but broadcast compatible arrays. Given 7x5, 7x1, 1x5 and 1x1 arrays I want to do something like

 a475 = mkarr([a75, a71, a15, a11])

where a455 will be a 4x7x5 array. Ideally I would also like to be able to do

 a2275 = mkarr([[a75, a71], [a15, a11]])

to get a 2x2x7x5 array.

What is the most concise way to express these operation in numpy?

You can use np.broadcast_arrays :

a475 = np.stack(np.broadcast_arrays(a75, a71, a15, a11))

Note that this creates views of the original arrays, so it should not entail any extra memory usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM