简体   繁体   中英

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

xy = 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-29-422790e30961> in <module>
     32             xm = [xm,xd,xw]
     33 
---> 34 xy = abs(np.amin(xm)) + xm

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

I should also add that the above Python code is my attempt at converting a MATLAB code to Python Code. 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

>> 

xm is a list of two lists

xm[0] has 120 elements, xm[1] has 6 elements

Since xm[0].shape.= xm[1].shape

you can't run np.amin(xm) but you could run np.amin(xm[0]) and np.amin(xm[1]) to get the absolute min value for each list and get

temp = [np.amin(xm[0]), np.amin(xm[1])] then np.amin(temp)

The xw should be instantiated at the start inside the mainFor loop no? not outside of it, to mimic the Matlab's scope setup.

xd = np.flip(xa)
for i in range(1,n-1):
    xw = [] # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    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])

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

I think your main issue is, that in Matlab [xd, xw] concatenates the two matrices xd and xw while the replacement you chose in Python np.array([xd, xw]) creates an array of arrays. So you need to use np.concatenate . The following code works for me:

xd = np.flip(xa)
for i in range(0, n - 1):
    if i == 0:
        pass
    else:
        xd = xd - 1
    xw = []
    for f in np.arange(1, ts):
        mini = np.amin(xd)
        xw.append(mini)
    if i == 0:
        xm = np.concatenate((xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)
    else:
        xm = np.concatenate((xm, xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)

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

However, in the output there still seems to be a little difference due to python starting indexing with 0 and Matlab with 1. Depending on what you want to use the code for, you might want to investigate this.

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