0

I got a little problem in putting the code from matlab into python, i know how to make loops and stuff but the double equal sign is the same as function is member and I have no idea how to put it in python

for i=1:49
    if path==var(path(1),i) == 0 & var(path(1),i) ~= 0
        path(1,2) = var(r,i);
        var2(i,1:2)  = path;
        path(1,1:2);
        a = a+1;
        two_connections(a,:) = path;
3
  • What is path? What does var(..., i) do? (The docs don't allow that construction: mathworks.com/help/techdoc/ref/var.html.) What is this code means to do? Commented May 5, 2011 at 9:35
  • == has the same meaning in Matlab as in Python. Your question is unclear Commented May 5, 2011 at 9:48
  • @katrielalex: I'm guessing var in this case is an array that he defined elsewhere, rather than the function. Commented May 5, 2011 at 14:56

1 Answer 1

1

The double double equal sign in Matlab tests whether each value is equivalent.

In other words (a==b==c) will evaluate to 1 if a,b,c are equivalent, and 0 otherwise (even if a==b.)

It is sufficient to ensure that a==b and b==c (or a==b and a==c, etc.)

The tilde equals sign is simply "not equal to".

So your if statement would look like:

if (path == 0) and (var(path(1),i) == 0) and (var(path(1),i) != 0):
Sign up to request clarification or add additional context in comments.

4 Comments

Note that is and == is not equivalent. If you create a class Z with this method: def __eq__(self, other): return 0 == other, then Z() == 0 is True, while Z() is 0 is False. I think a is b is equivalent to id(a) == id(b).
That is a very good point and I didn't mean to be misleading. In this case, however, both statements will evaluate the same way.
-can you edit your answer so that "is" is not shown as equivalent to "==" ? Otherwise it deserves to be downvoted
@jsbueno -- I had thought about that, but didn't want to destroy the context of the comments -- but per your suggestion, I deleted it -- thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.