1

I am trying to convert a Matlab code to Python, and I'm facing a problem when I convert a line. Am I right or not? I don't know how to do assignment in Python.

Matlab:

 for j=1:a
     diff_a=zeros(1,4);
     diff_b=zeros(1,4);
     for i=1:4
         diff_a(i)=abs(ssa(j)-check(i));
         diff_b(i)=abs(ssb(j)-check(i));
     end
     [Y_a,I_a]=min(diff_a);
 end

Python:

for j in arange(0,a):
    diff_a=zeros(4)
    diff_b=zeros(4)
    for i in arange(0,4):
        diff_a[i]=abs(ssa[j]-check[i])
        diff_b[i]=abs(ssb[j]-check[i])
    [Y_a,I_a]=min(diff_a)

the last line gives this error:

TypeError: 'numpy.float64' object is not iterable

The problem is in the last line. diff_a is a complex number array. Sorry for not providing the whole code (it's too big).

3
  • The error is because python thinks you're trying to assign into two variables and you only have one value. Instead, just assign to a single variable or extract each piece using the real and imag methods. Commented Aug 1, 2013 at 15:37
  • Is it not diff_a.min() rather? Commented Aug 1, 2013 at 15:40
  • this also works fine ... Commented Aug 1, 2013 at 15:42

4 Answers 4

5

When you do [C,I] = min(...) in Matlab, it means that the minimum will be stored in C and the index of the minimum in I. In Python/numpy you need two calls for this. In your example:

Y_a, I_a = diff_a.min(), diff_a.argmin()

But the following is better code:

I_a = diff_a.argmin()
Y_a = diff_a[I_a]

Your code can be simplified a little more:

import numpy as np

for j in range(a):
    diff_a = np.abs(ssa[j] - check)
    diff_b = np.abs(ssb[j] - check)
    I_a = diff_a.argmin()
    Y_a = diff_a[I_a]
Sign up to request clarification or add additional context in comments.

Comments

2

You can simplify and increase your code performance doing:

diff_a = numpy.absolute( np.subtract.outer(ssa, check) )
diff_b = numpy.absolute( np.subtract.outer(ssb, check) )
I_a = diff_a.argmin( axis=1 )
Y_a = diff_a.min( axis=1 )

Here I_a and Y_a are arrays of shape (a,4) according to your code.

The error you are getting is because you are trying to unpack a numpy.float64 value when doing:

[Y_a,I_a]=min(diff_a)

since min() returns a single value

1 Comment

Note that the OP is using different indices for ssa and check. Also, I_a is supposed to be the argmin.
1

The problem is that you are return a single value min(diff_a) to a list [Y_a, I_a].

min(diff_a) finds the smallest value in the iterable, in this case diff_a. You can't assign one value to a list. Try something like

result = min(diff_a)

or just

print min(diff_a)

Comments

0
min(diff_a)

returns minimum object from iterable which in your case is apparently a float, you can't iterate over a float, yet this is what you are trying to do in you assignement:

[Y_a,I_a]=min(diff_a)

you're essentialy trying to create a new list of two items Y_a and I_a from just one value - which is the result of applying min function to diff_a list.

If you need to get two smallest values from diff_a try this:

[Y_a,I_a]= sorted(diff_a)[:2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.