简体   繁体   中英

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

So this is my python code

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

When I run it there comes up a big pile of error block. I know that arrays must have the same rank to perform mathematical operations but I don't know how to do that here.

This is error block that comes up when I run the code

---------------------------------------------------------------------------
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,)
 

Heres the Matlab code

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);

And this is the output I get from running the MATLAB code which is also the output I'm expecting from my python code.

>>
  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

>> 

When you ran this code, did you git the ragged array warning? If so, why did you ignore it? Or are you running such old numpy that it didn't give the warning?

<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 is object dtype containing one array and one list. Their lengths match the ones stated in the error: shapes (120,) (6,)

You can't do np.min on such an array.

I won't try to sort out the logic of your xm constructor, but I see that you use xw as list and list append, but xm as array and np.array([...]) to make new values in the loop. Repeated np.array in a loop is not a good idea; it is slow and prone to errors. The same applies to use np.concatenate (or one its derivatives) in a loop. If you are going to work iteratively, stick with lists, and make the arrays, if needed, with one call at the end.

Back when I worked with MATLAB, around version 3.9, we tried to avoid loops because they were slow. Since then MATLAB has add jit compilation so there isn't much of a time penalty. In numpy it is still best to avoid loops, or to use numba to compile them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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