简体   繁体   English

我需要一种方法来摆脱 numpy 数组中的大括号

[英]I need a method to get rid of curly brackets in my numpy array

I have a 61-by-61 matrix (numpy array) that includes double values (complex numbers).我有一个包含双精度值(复数)的 61×61 矩阵(numpy 数组)。 I want to save my matrix as a CSV file, then use it in Matlab.我想将我的矩阵保存为 CSV 文件,然后在 Matlab 中使用它。 But all the elements have curly brackets "(, )".但是所有元素都有大括号“(,)”。 For example, an element is like:例如,一个元素是这样的:

 (3.2-0.4j) 

I want all my elements to be without curly brackets;我希望我的所有元素都没有大括号; I mean:我是说:

3.2-0.4j
In [213]: arr = np.arange(3.)+np.arange(4)[:,None]*1j                                   
In [214]: arr                                                                           
Out[214]: 
array([[0.+0.j, 1.+0.j, 2.+0.j],
       [0.+1.j, 1.+1.j, 2.+1.j],
       [0.+2.j, 1.+2.j, 2.+2.j],
       [0.+3.j, 1.+3.j, 2.+3.j]])

How you attempted it (probably):你是如何尝试的(可能):

In [220]: np.savetxt('test.txt',arr, delimiter=',', fmt='%3d')                          
In [221]: cat test.txt                                                                  
 (  0+  0j), (  1+  0j), (  2+  0j)
 (  0+  1j), (  1+  1j), (  2+  1j)
 (  0+  2j), (  1+  2j), (  2+  2j)
 (  0+  3j), (  1+  3j), (  2+  3j)

With the complex format as suggested in the docs (an exact copy):使用文档中建议的复杂格式(精确副本):

In [223]: np.savetxt('test.txt',arr, fmt=' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej')   
                                                                
In [224]: cat test.txt                                                                  
 0.0000e+00 +0.0000e+00j 1.0000e+00 +0.0000e+00j 2.0000e+00 +0.0000e+00j
 0.0000e+00 +1.0000e+00j 1.0000e+00 +1.0000e+00j 2.0000e+00 +1.0000e+00j
 0.0000e+00 +2.0000e+00j 1.0000e+00 +2.0000e+00j 2.0000e+00 +2.0000e+00j
 0.0000e+00 +3.0000e+00j 1.0000e+00 +3.0000e+00j 2.0000e+00 +3.0000e+00j

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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