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;
zellus
9,5825 gold badges41 silver badges58 bronze badges
1 Answer 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):
answered May 5, 2011 at 9:48
jedwards
30.4k3 gold badges69 silver badges94 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Lauritz V. Thaulow
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).jedwards
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.
jsbueno
-can you edit your answer so that "is" is not shown as equivalent to "==" ? Otherwise it deserves to be downvoted
jedwards
@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!
default
path? What doesvar(..., i)do? (The docs don't allow that construction: mathworks.com/help/techdoc/ref/var.html.) What is this code means to do?==has the same meaning in Matlab as in Python. Your question is unclearvarin this case is an array that he defined elsewhere, rather than the function.