Home > Blockchain >  unsupported operand type(s) for : 'int' and 'tuple'... How can I solve this iss
unsupported operand type(s) for : 'int' and 'tuple'... How can I solve this iss

Time:09-29

I have the followig code where I am doing some interpolation but I am getting the above mentioned error in this line of code tem=p.gamma_dB_min p.delta_dB_gam*y when I run the last line of this code. could anybody solve this problem. thank you

def lin_interp(p,y1,yn):
    N = 28
    n = np.arange(1,N)
    y = ( [y1   (yn-y1)/N*n], [yn] )
    tem = p.gamma_dB_min   p.delta_dB_gam*y
    gamma_dB = np.reshape(tem, [1, N*len(yn)])
    return gamma_dB

v1=y_test[1:]
v2=y_pred[1:]
gamma_DRNN1=[y_pred[1],lin_interp(p,v1,v2)]

I am trying to convert the following MATLAB code into python:

function [gamma_dB] = lin_interp_cqi(p,Y1,YN)
N=28;
n=(1:(N-1)).';
y=[(YN-Y1)/N.*n Y1;YN];
    
gamma_dB=reshape(p.gamma_dB_min p.delta_dB_gam*y,[1,N*length(YN)]);    

end

CodePudding user response:

Well from the error message, its becuase p.gamma_dB_min is an int and p.delta_dB_gam*y is a tuple (or vice-versa), and this opperation is unsupported e.g. you can't do 1 (1, ), but you can do 1 (1).

Make sure p.gamma_dB_min & p.delta_dB_gam*y are in the form you expect. Beyond that, it's hard to debug based on the information you provided.

CodePudding user response:

You need to create NumPy arrays wherever you deal with more than one value. Using NumPy arrays will make your code more similar to the MATLAB code, and thus will make translation easier. In particular,

y = [(YN-Y1)/N.*n Y1; YN];

would be translated to Python as

y = np.array([(YN-Y1)/N.*n Y1, YN])

CodePudding user response:

As other answers have said, this is because p.gamma_dB_min is an int and p.delta_dB_gam*y is a tuple, and adding an int to a tuple is not a supported operation in Python. If what you are trying to do is add a number to each element of a tuple, you can use Python's map() function, for example:

my_number = 7
my_tuple = (1,2,3,4)
result = tuple(map(lambda n: n my_number, my_tuple))
# Giving result = (8,9,10,11)
  • Related