Python subtleties for MATLAB users
Question:
What are some of the most basic things that a programmer who is used to coding in MATLAB needs to be cautious about, when beginning to code in Python? I am not asking for an entire list of differences between MATLAB and Python, just a few very basic things that can save a beginner a few hours of debugging.
An example would be how someone used to MATLAB’s 1:N
may make a mistake in python by using range(1,N)
Differences in concepts would be more helpful than absolute differences in specific commands.
Answers:
The NumPy site has an excellent list, which I won’t reproduce in full here. NumPy provides basically all the basic functionality of MATLAB for Python users, so it is highly recommended if you’re coming to Python from MATLAB.
Some highlights:
- INDEXING: MATLABĀ® uses one based indexing, so the initial element of a sequence has index 1. Python uses zero based indexing, so the initial element of a sequence has index 0. Confusion and flamewars arise because each has advantages and disadvantages. One based indexing is consistent with common human language usage, where the "first" element of a sequence has index 1. Zero based indexing simplifies indexing. See also a text by prof.dr. Edsger W. Dijkstra.
- LOGICOPS: & or | in Numpy is bitwise AND/OR, while in Matlab & and | are logical AND/OR. The difference should be clear to anyone with significant programming experience. The two can appear to work the same, but there are important differences. If you would have used Matlab’s & or | operators, you should use the Numpy ufuncs logical_and/logical_or.
What are some of the most basic things that a programmer who is used to coding in MATLAB needs to be cautious about, when beginning to code in Python? I am not asking for an entire list of differences between MATLAB and Python, just a few very basic things that can save a beginner a few hours of debugging.
An example would be how someone used to MATLAB’s 1:N
may make a mistake in python by using range(1,N)
Differences in concepts would be more helpful than absolute differences in specific commands.
The NumPy site has an excellent list, which I won’t reproduce in full here. NumPy provides basically all the basic functionality of MATLAB for Python users, so it is highly recommended if you’re coming to Python from MATLAB.
Some highlights:
- INDEXING: MATLABĀ® uses one based indexing, so the initial element of a sequence has index 1. Python uses zero based indexing, so the initial element of a sequence has index 0. Confusion and flamewars arise because each has advantages and disadvantages. One based indexing is consistent with common human language usage, where the "first" element of a sequence has index 1. Zero based indexing simplifies indexing. See also a text by prof.dr. Edsger W. Dijkstra.
- LOGICOPS: & or | in Numpy is bitwise AND/OR, while in Matlab & and | are logical AND/OR. The difference should be clear to anyone with significant programming experience. The two can appear to work the same, but there are important differences. If you would have used Matlab’s & or | operators, you should use the Numpy ufuncs logical_and/logical_or.