简体   繁体   English

嗨,我尝试用 python 编写这段代码,但是我有一个错误,我希望有人可以帮助我

[英]Hi, I have tried to write this code in python, but i have a error, i hope that someone could help me

This is my code in python, the dimension of sx should be of 100X4 and sy 100X1 by the multiplication (sx) (B) (sy).这是我在 python 中的代码,sx 的维度应该是 100X4 和 sy 100X1 乘法 (sx) (B) (sy)。

import numpy as np                                                                             
  
B= [[-6.08066634428988e-10, -8.61023850910464e-11, 5.48222828615260e-12, -9.49229025004441e-14], 
    [-3.38148313553674e-11, 6.47759097087283e-12, 1.14900158474371e-13, -5.70078947874486e-15], 
    [-2.55893304237669e-13, -1.40941560399352e-13, 5.76510238931847e-15, -5.52980385181738e-17], 
    [3.39795122177475e-15, 7.95704191204353e-16, -5.31260642039813e-17, 7.83532802015832e-19]]                                                                         

[X, Y] = np.meshgrid(np.arange(0, 3, 0.01*3),np.arange(0, 15, 0.01*(15)))                           
sx=[]                                                                                                              
sy=[]                                                                                                               
F=[]                                                                                            
for i in range(len(X)):
    for j in range(len(X)):
        for k in range(len(B)):
            sx[i,k].append(X[i,j]**k)
        for l in range(len(B)):
            sy[l].append((Y[i,j]**l))
        F[i,j] = sx*B*sy 

The error:错误:

sx[i,k].append(X[i,j]**k) TypeError: list indices must be integers or slices, not tuple   

MATLAB code copied from comment (guess as to formatting)从注释中复制的 MATLAB 代码(猜测格式)

 [x,y]=meshgrid(0:0.01*3:3,0:0.01*15:15); 
 for i=1:size(x) 
     for j=1:size(x) 
         for k=0:size(B) -1 
             sx(1,k+1)=(x(i,j)^k); 
         end 
         for k=0:size(B) -1 
             sy(k+1,1)=(y(i,j)^k); 
         end 
      G(i,j)=sx*B*sy; 
   end 
end 

If sx or X is a 2D list then indices must be [i][j] .如果sxX是二维列表,则索引必须为[i][j] If you're trying to append to two indices i and j then it should be separate calls to append.如果您尝试附加到两个索引ij那么它应该是单独的 append 调用。

In an Octave session:在八度音程中:

B =

  -6.0807e-10  -8.6102e-11   5.4822e-12  -9.4923e-14
  -3.3815e-11   6.4776e-12   1.1490e-13  -5.7008e-15
  -2.5589e-13  -1.4094e-13   5.7651e-15  -5.5298e-17
   3.3980e-15   7.9570e-16  -5.3126e-17   7.8353e-19

>>
>>  [x,y]=meshgrid(0:0.01*3:3,0:0.01*15:15);
>>  for i=1:size(x)
     for j=1:size(x)
         for k=0:size(B) -1
             sx(1,k+1)=(x(i,j)^k);
         end
         for k=0:size(B) -1
             sy(k+1,1)=(y(i,j)^k);
         end
      G(i,j)=sx*B*sy;
   end
end

produces产生

x, y, G (101 x 101)
>> sx         (1,4)
sx =
    1    3    9   27
>> sy          (4,1)
sy =
      1
     15
    225
   3375

So the G element is (1,4) * (4,4) * (4,1) => (1,1)所以G元素是 (1,4) * (4,4) * (4,1) => (1,1)

Looks like I should be able to make a看起来我应该能够做一个

In [100]: B= [[-6.08066634428988e-10, -8.61023850910464e-11, 5.48222828615260e-12, -9.49229025004441e-14],
     ...:     [-3.38148313553674e-11, 6.47759097087283e-12, 1.14900158474371e-13, -5.70078947874486e-15],
     ...:     [-2.55893304237669e-13, -1.40941560399352e-13, 5.76510238931847e-15, -5.52980385181738e-17],
     ...:     [3.39795122177475e-15, 7.95704191204353e-16, -5.31260642039813e-17, 7.83532802015832e-19]]
     ...: 
In [101]: B = np.array(B)
In [106]: [X, Y] = np.meshgrid(np.linspace(0, 3, 101),np.linspace(0, 15, 101),indexing='ij')
In [107]: X.shape
Out[107]: (101, 101)
In [108]: k = np.arange(0,4)
In [109]: k
Out[109]: array([0, 1, 2, 3])

In [110]: SX = X[:,:,None]**k         # (101,101,4)
In [111]: SY = Y[:,:,None]**k
In [114]: G = np.einsum('ijk,kl,ijl->ij',SX,B,SY)
In [115]: G.shape
Out[115]: (101, 101)

Allowing for the "F" order of MATLAB (ie. transpose), looks like these results match:考虑到 MATLAB 的“F”阶(即转置),看起来这些结果匹配:

>> G(1,1)
ans = -0.00000000060807
In [118]: G[0,0]
Out[118]: -6.08066634428988e-10
>> G(50,23)
ans = -0.00000000097117
In [119]: G[22,49]
Out[119]: -9.71172989297259e-10

With broadcasting I don't to make the meshgrid arrays通过broadcasting我不会制作meshgrid阵列

In [121]: x, y = np.linspace(0,3,101), np.linspace(0,15,101)
In [124]: sx = x[:,None]**k
In [125]: sy = y[:,None]**k
In [126]: sx.shape
Out[126]: (101, 4)

In [129]: g = sx@B@sy.T
In [130]: g.shape
Out[130]: (101, 101)
In [131]: np.allclose(G,g)
Out[131]: True

Here I'm doing a matrix product of在这里,我正在做一个矩阵乘积

(101,4) (4,4) (4,100) => (101,101)

暂无
暂无

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

相关问题 这是正确的吗? 我希望有人可以帮助我嘿嘿 - is it correct? i hope someone can help me hehe 嗨,我是SQL / python的新手,有人可以帮助我使用python命令搜索数据库,谢谢:) - Hi, I'm new to SQL/python, could someone please help me with searching databases using python commands, thanks :) 我有这段代码,试图通过捕获视频来运行社会隔离识别,但我有这个错误。 你可以帮帮我吗? - I have this code, trying to run a social isolation identification by capturing video, but I have this error. Could you help me? 有人可以向我解释此Python代码中我在做什么错,并帮我弄清楚吗? - Could someone explain to me what I'm doing wrong in this Python Code and help me figure it out? 嗨,我有一个 python 脚本可以使用 openpyxl 处理 excel 数据,但是在 2500 行之后我收到 Memory 错误。 谁可以帮我这个事? - Hi, I have a python script to work with excel data using openpyxl ,but after 2500 rows i am getting Memory Error. Can anyone help me on this? 我该如何编写这个python输入代码? 我所拥有的给了我一条错误消息 - How can I write this python input code? What I have gives me an error message 可以优化给定的代码吗,我尝试通过引用不同的站点来做到这一点,但它总是向我显示错误 - can the given code be optimized, i have tried doing it by referring different sites but it always shows me error 如何编写这个 python 代码。 我试过但没有用 - How to write this python code. I have tried it but didn't work 我的代码有问题。 我需要解决这个错误。 帮我 - I have a problem with a code. I need to solve this error. Help me 有人可以帮我找出这段代码中的错误吗? - Could someone help me find the error in this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM