簡體   English   中英

MATLAB 到 PYTHON 轉換 [ValueError: 操作數無法與形狀 (120,) (6,) 一起廣播]

[英]MATLAB TO PYTHON conversion [ValueError: operands could not be broadcast together with shapes (120,) (6,) ]

所以這是我的 python 代碼

import numpy as np
n = 3                          
T = 100                                            
ts = .2*(100/(2*n-3))                              
tv = .6*((100-((2*n-3)*ts))/(2*(n-1)))             
m1 =   np.arange(0,tv,0.1); 
x1 = 0.5*(1-(np.cos(np.pi*(m1/tv)))) 
xa = x1;

#%travelling from right to left

xd = np.flip(xa)
xw = []
for i in range(1,n-1):
    if i==1:
        pass
    else:
        xd = xd-1
        
    #%standing at one point
    for f in np.arange(1,ts):
        mini = np.amin(xd)
        xw.append(mini)
        if i==1:
            xm=np.array([xd,xw])
        else:
            xm = np.array([xm,xd,xw])

xm = abs(np.amin(xm)) + xm

當我運行它時,會出現一大堆錯誤塊。 我知道 arrays 必須具有相同的等級才能執行數學運算,但我不知道如何在此處執行此操作。

這是我運行代碼時出現的錯誤塊

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-64722470b31e> in <module>
     32             xm = np.array([xm,xd,xw])
     33 
---> 34 xm = abs(np.amin(xm)) + xm
     35 
     36 

<__array_function__ internals> in amin(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in amin(a, axis, out, keepdims, initial, where)
   2856     6
   2857     """
-> 2858     return _wrapreduction(a, np.minimum, 'min', axis, None, out,
   2859                           keepdims=keepdims, initial=initial, where=where)
   2860 

~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     85                 return reduction(axis=axis, out=out, **passkwargs)
     86 
---> 87     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     88 
     89 

ValueError: operands could not be broadcast together with shapes (120,) (6,)
 

這是 Matlab 代碼

xd = flip(xa);

for i= 1:n-1
    if i==1
    else
        xd = xd-1;
    end%standing at one point
    for f = 1:ts
        xw(f) = min(xd);
    end
    if i==1
        xm=[xd,xw];
    else
        xm = [xm,xd,xw];
    end
end
xm = abs(min(xm))+xm;
disp(xm);

這是我從運行 MATLAB 代碼得到的 output,這也是我期望從我的 python 代碼得到的 output。

>>
  Columns 1 through 13

    2.0000    1.9998    1.9993    1.9985    1.9973    1.9957    1.9938    1.9916    1.9891    1.9862    1.9830    1.9794    1.9755

  Columns 14 through 26

    1.9713    1.9668    1.9619    1.9568    1.9513    1.9455    1.9394    1.9330    1.9263    1.9193    1.9121    1.9045    1.8967

  Columns 27 through 39

    1.8886    1.8802    1.8716    1.8627    1.8536    1.8442    1.8346    1.8247    1.8147    1.8044    1.7939    1.7832    1.7723

  Columns 40 through 52

    1.7612    1.7500    1.7386    1.7270    1.7153    1.7034    1.6913    1.6792    1.6669    1.6545    1.6420    1.6294    1.6167

  Columns 53 through 65

    1.6040    1.5911    1.5782    1.5653    1.5523    1.5392    1.5262    1.5131    1.5000    1.4869    1.4738    1.4608    1.4477

  Columns 66 through 78

    1.4347    1.4218    1.4089    1.3960    1.3833    1.3706    1.3580    1.3455    1.3331    1.3208    1.3087    1.2966    1.2847

  Columns 79 through 91

    1.2730    1.2614    1.2500    1.2388    1.2277    1.2168    1.2061    1.1956    1.1853    1.1753    1.1654    1.1558    1.1464

  Columns 92 through 104

    1.1373    1.1284    1.1198    1.1114    1.1033    1.0955    1.0879    1.0807    1.0737    1.0670    1.0606    1.0545    1.0487

  Columns 105 through 117

    1.0432    1.0381    1.0332    1.0287    1.0245    1.0206    1.0170    1.0138    1.0109    1.0084    1.0062    1.0043    1.0027

  Columns 118 through 130

    1.0015    1.0007    1.0002    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.9998    0.9993

  Columns 131 through 143

    0.9985    0.9973    0.9957    0.9938    0.9916    0.9891    0.9862    0.9830    0.9794    0.9755    0.9713    0.9668    0.9619

  Columns 144 through 156

    0.9568    0.9513    0.9455    0.9394    0.9330    0.9263    0.9193    0.9121    0.9045    0.8967    0.8886    0.8802    0.8716

  Columns 157 through 169

    0.8627    0.8536    0.8442    0.8346    0.8247    0.8147    0.8044    0.7939    0.7832    0.7723    0.7612    0.7500    0.7386

  Columns 170 through 182

    0.7270    0.7153    0.7034    0.6913    0.6792    0.6669    0.6545    0.6420    0.6294    0.6167    0.6040    0.5911    0.5782

  Columns 183 through 195

    0.5653    0.5523    0.5392    0.5262    0.5131    0.5000    0.4869    0.4738    0.4608    0.4477    0.4347    0.4218    0.4089

  Columns 196 through 208

    0.3960    0.3833    0.3706    0.3580    0.3455    0.3331    0.3208    0.3087    0.2966    0.2847    0.2730    0.2614    0.2500

  Columns 209 through 221

    0.2388    0.2277    0.2168    0.2061    0.1956    0.1853    0.1753    0.1654    0.1558    0.1464    0.1373    0.1284    0.1198

  Columns 222 through 234

    0.1114    0.1033    0.0955    0.0879    0.0807    0.0737    0.0670    0.0606    0.0545    0.0487    0.0432    0.0381    0.0332

  Columns 235 through 247

    0.0287    0.0245    0.0206    0.0170    0.0138    0.0109    0.0084    0.0062    0.0043    0.0027    0.0015    0.0007    0.0002

  Columns 248 through 254

         0         0         0         0         0         0         0

>> 

當你運行這段代碼時,你有沒有 git 參差不齊的ragged array警告? 如果是這樣,你為什么忽略它? 還是您正在運行這么舊的numpy以致於它沒有發出警告?

<ipython-input-1-7dab9a6384c7>:24: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  xm=np.array([xd,xw])
In [2]: xm
Out[2]: 
array([array([9.99828662e-01, 9.99314767e-01, 9.98458667e-01, 9.97260948e-01,
              9.95722431e-01, 9.93844170e-01, 9.91627454e-01, 9.89073800e-01,
...
              1.54133313e-03, 6.85232623e-04, 1.71337512e-04, 0.00000000e+00]),
       list([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])], dtype=object)

In [3]: xm.shape
Out[3]: (2,)
In [4]: xm.dtype
Out[4]: dtype('O')
In [5]: xm[0].shape
Out[5]: (120,)
In [7]: len(xm[1])
Out[7]: 6

xm是 object dtype,包含一個數組和一個列表。 它們的長度與錯誤中所述的長度相匹配: shapes (120,) (6,)

您不能在這樣的數組上執行np.min

我不會嘗試理清你的xm構造函數的邏輯,但我看到你使用xw作為列表和列表 append,但xm作為數組和np.array([...])在循環中生成新值. 在循環中重復np.array不是一個好主意; 它很慢並且容易出錯。 這同樣適用於在循環中使用np.concatenate (或其衍生物之一)。 如果您要反復工作,請堅持使用列表,並在需要時撥打 arrays,最后打一個電話。

當我使用 MATLAB 時,大約是 3.9 版,我們試圖避免循環,因為它們很慢。 從那時起 MATLAB 添加jit編譯,因此沒有太多時間損失。 numpy還是最好避免循環,或者用numba來編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM