How to interleave numpy.ndarrays?

2024/10/11 22:23:23

I am currently looking for method in which i can interleave 2 numpy.ndarray. such that

>>> a = np.random.rand(5,5)
>>> print a
[[ 0.83367208  0.29507876  0.41849799  0.58342521  0.81810562][ 0.31363351  0.69468009  0.14960363  0.7685722   0.56240711][ 0.49368821  0.46409791  0.09042236  0.68706312  0.98430387][ 0.21816242  0.87907115  0.49534121  0.60453302  0.75152033][ 0.10510938  0.55387841  0.37992348  0.6754701   0.27095986]]
>>> b = np.random.rand(5,5)
>>> print b
[[ 0.52237011  0.75242666  0.39895415  0.66519185  0.87043142][ 0.08624797  0.66193953  0.80640822  0.95403594  0.33977566][ 0.13789573  0.84868366  0.09734757  0.06010175  0.48043968][ 0.28871551  0.62186888  0.44603741  0.3351644   0.6417847 ][ 0.85745394  0.93179792  0.62535765  0.96625077  0.86880908]]
>>> 

print c shoule be interleaving each row both matrices

[ 0.83367208   0.52237011  0.29507876 0.75242666 0.41849799 0.39895415 0.58342521 0.66519185 0.81810562 0.87043142]

I have three in total which should be interleaved, but i guess it would be easier to do it two at a time..

but how do i do it easily.. I read some method which used arrays, but i am not sure to do it with ndarrays?

Answer

Stack those along the third axis with np.dstack and reshape back to 2D -

np.dstack((a,b)).reshape(a.shape[0],-1)

With three arrays or even more number of arrays, simply add in those. Thus, for three arrays, use : np.dstack((a,b,c)) and reshape with c being the third array.

Sample run -

In [99]: a
Out[99]: 
array([[8, 4, 0, 5, 6],[0, 2, 3, 0, 6],[4, 4, 0, 6, 5],[7, 5, 0, 7, 0],[6, 7, 4, 7, 2]])In [100]: b
Out[100]: 
array([[3, 5, 8, 6, 5],[5, 6, 8, 8, 4],[8, 3, 3, 3, 5],[2, 1, 1, 1, 3],[5, 7, 7, 5, 7]])In [101]: np.dstack((a,b)).reshape(a.shape[0],-1)
Out[101]: 
array([[8, 3, 4, 5, 0, 8, 5, 6, 6, 5],[0, 5, 2, 6, 3, 8, 0, 8, 6, 4],[4, 8, 4, 3, 0, 3, 6, 3, 5, 5],[7, 2, 5, 1, 0, 1, 7, 1, 0, 3],[6, 5, 7, 7, 4, 7, 7, 5, 2, 7]])
https://en.xdnf.cn/q/69722.html

Related Q&A

Object is not subscripable networkx

import itertools import copy import networkx as nx import pandas as pd import matplotlib.pyplot as plt #-- edgelist = pd.read_csv(https://gist.githubusercontent.com/brooksandrew /e570c38bcc72a8d1024…

WTForms : How to add autofocus attribute to a StringField

I am rather new to WTForms, Flask-WTF. I cant figure out how to simply add the HTML5 attribute "autofocus" to one of the form field, from the form definition. I would like to do that in the P…

Image rotation in Pillow

I have an image and I want to transpose it by 30 degrees. Is it possible to do by using something like the following?spinPicture003 = Picture003.transpose(Image.Rotate_30)

Python code to calculate angle between three points (lat long coordinates)

Can anybody suggest how to calculate angle between three points (lat long coordinates)A : (12.92473, 77.6183) B : (12.92512, 77.61923) C : (12.92541, 77.61985)

z3: solve the Eight Queens puzzle

Im using Z3 to solve the Eight Queens puzzle. I know that each queen can be represented by a single integer in this problem. But, when I represent a queen by two integers as following:from z3 import *X…

Image skewness kurtosis in python

Is there a python package that will provide me a way to clacluate Skewness and Kurtosis of an image?. Any example will be great.Thanks a lot.

Python: Getting all the items out of a `threading.local`

I have a threading.local object. When debugging, I want to get all the objects it contains for all threads, while I am only on one of those threads. How can I do that?

Why tuple is not mutable in Python? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why th…

Display Django form fields on the same line

I would like to display two form fields on the same line and not each one after the other one.For the moment, I get:Choice a theme :. Datasystems. CamerounBut I would like to display this form like:Cho…

How can I use the index array in tensorflow?

If given a matrix a with shape (5,3) and index array b with shape (5,), we can easily get the corresponding vector c through,c = a[np.arange(5), b]However, I cannot do the same thing with tensorflow,a …