- 61.4k
- 10
- 76
- 135
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In Python xx>0.002 is an array of Booleans, False and True and np.diff treats the Boolean values as such, in MatlabTMMatlab xx>0.002 is a matrix of logical values as well but diff converts them to 0 and 1 before taking the differences and this imply that we have more possibilities in MatlabTMMatlab
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In Python xx>0.002 is an array of Booleans, False and True and np.diff treats the Boolean values as such, in MatlabTM xx>0.002 is a matrix of logical values as well but diff converts them to 0 and 1 before taking the differences and this imply that we have more possibilities in MatlabTM
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In Python xx>0.002 is an array of Booleans, False and True and np.diff treats the Boolean values as such, in Matlab xx>0.002 is a matrix of logical values as well but diff converts them to 0 and 1 before taking the differences and this imply that we have more possibilities in Matlab
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In MatlabTMPython xx>0.002 is a binary integer matrixan array of Booleans, its values 0False and True and 1np.diff — otohtreats the Boolean values as such, in PythonMatlabTM xx>0.002 is an arraya matrix of Booleans,logical values as well but Falsediff andconverts them to True0. This imply that, and 1 before taking the differences, and this imply that we have more possibilities in MatlabTM
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In MatlabTM xx>0.002 is a binary integer matrix, its values 0 and 1 — otoh in Python xx>0.002 is an array of Booleans, False and True. This imply that, taking the differences, we have more possibilities in MatlabTM
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In Python xx>0.002 is an array of Booleans, False and True and np.diff treats the Boolean values as such, in MatlabTM xx>0.002 is a matrix of logical values as well but diff converts them to 0 and 1 before taking the differences and this imply that we have more possibilities in MatlabTM
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...
TL;DR Python is different, you can make Python like Matlab, in this case I prefer the approach of Python.
In MatlabTM xx>0.002 is a binary integer matrix, its values 0 and 1 — otoh in Python xx>0.002 is an array of Booleans, False and True. This imply that, taking the differences, we have more possibilities in MatlabTM
In [15]: for a, b in ((0,0), (0,1), (1,0), (1,1)): print(np.diff((a,b)))
[0]
[1]
[-1]
[0]
In [16]: f, t = False, True
...: for a, b in ((f,f), (f,t), (t,f), (t,t)): print(np.diff((a,b)))
[False]
[ True]
[ True]
[False]
When I plot xx and diff(xx>0.02) in Matlab (oh well, in Octave...) I have
When I plot xx and np.diff(xx>0.02) in Python+Numpy+Matplotlib I get
To have exactly the results of Matlab we can convert the boolean array to an array of floats, just multiplying by 1.0 is OK — so this is the plot of xx and np.diff( 1.0*(xx>0.02) )
If the aim of the OP is to show where the signal is larger than 0.02 I dare say that the native Python (no conversion to floats) is better at that...