numpy的:创建2以假乱真一系列复杂? [英] Numpy: Creating a complex array from 2 real ones?

查看:174
本文介绍了numpy的:创建2以假乱真一系列复杂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发誓,这应该是这么容易......为什么不能? (

其实,我想结合2件在同一阵列,使一系列复杂的:

 数据[:,:,:,0],数据[:,:,:,1]

这些不工作:

  X = np.complex(数据[:,:,:,0],数据[:,:,:,1])
X =络合物(数据[:,:,:,0],数据[:,:,:,1])

我缺少的东西吗?难道numpy的不喜欢在复数执行阵列功能呢?这里的错误:

 类型错误:只有长度为1的阵列可以转换到Python标量


解决方案

这似乎做你想要什么:

  numpy.apply_along_axis(拉姆达ARGS:复杂的(*参数)],3,数据)

下面是另一种解决方案:

  numpy.vectorize(复)(数据[...,0],数据[...,1])#省略号相当于这里:,:,: 

和另一个简单的解决方法:

 数据[...,0] + 1J *数据[...,1]

PS :如果你想节省内存(没有中间排列):

 结果= 1J *数据[...,1];结果+ =数据[...,0]

下面的开发者的解决方案也不错。

I swear this should be so easy... Why is it not? :(

In fact, I want to combine 2 parts of the same array to make a complex array:

Data[:,:,:,0] , Data[:,:,:,1]

These don't work:

x = np.complex(Data[:,:,:,0], Data[:,:,:,1])
x = complex(Data[:,:,:,0], Data[:,:,:,1])

Am I missing something? Does numpy not like performing array functions on complex numbers? Here's the error:

TypeError: only length-1 arrays can be converted to Python scalars

解决方案

This seems to do what you want:

numpy.apply_along_axis(lambda args: [complex(*args)], 3, Data)

Here is another solution:

numpy.vectorize(complex)(Data[...,0], Data[...,1])  # The ellipsis is equivalent here to ":,:,:"

And yet another simpler solution:

Data[...,0] + 1j * Data[...,1]

PS: If you want to save memory (no intermediate array):

result = 1j*Data[...,1]; result += Data[...,0]

devS' solution below is also good.

这篇关于numpy的:创建2以假乱真一系列复杂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆