Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

Eight Advantages of Python Over Matlab

$
0
0

By Riddhiman

The original post is here. Dr. Phillip Feldman has kindly agreed to let us repost his thoughts on some key differences between Matlab and Python.

Python code tends to be more compact and more readable than Matlab code. There are several reasons for this:

indentation

Unlike Matlab, which uses end statements as closures (i.e., to indicate the end of a block), Python determines the scope of a block based on its indentation. This forces Python programmers to indent code blocks, which is good practice anyway. (And, unlike programmers working in most other languages, no Python programmer ever wastes time hunting for a missing end statement).

[] vs ()

Like almost every programming language other than Matlab, Python uses square brackets for indexing and parentheses for function and method calls. Matlab uses parentheses for both. Python’s use of square brackets for indexing is important for readability, and also makes life easier for programmers who must work with multiple languages. Matlab’s use of parentheses for both indexing and function calls can make Matlab code hard to understand and is a frequent source of bugs.

augmented assignments

Support for augmented assignments tends to make Python code less verbose than Matlab code. Compare the following two fragments of code, which collect counts for a 2-D histogram:

Matlab:

count(max(floor(x),1),max(floor(y),1))= …
count(max(floor(x),1),max(floor(y),1)) + 1

Python:

count[max(floor(x),0),max(floor(y),0)]+= 1

Note that the Matlab code is roughly twice as long. The chances of a typographical error are greater when entering this code, and it takes longer for someone to read and understand it. In general, Python code is almost always smaller (in terms of lines of code, assuming only one statement and no more than 80 characters per line) than Matlab code that performs the same task. For code that performs primarily numerical operations, one can expect a Python implementation to be 10 to 20 percent smaller than equivalent Matlab code. For …read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles