Differences Between matlab and python

Source: Internet
Author: User
Tags bitwise operators

More and more people in the academic world use the Python language instead of Matlab as the main programming tool for research. The python language looks a bit similar to the matlab language. For users who are used to Matlab, conversion to the Python language should not be difficult, but there are some key points to note, that is, the differences between the two languages. Having mastered the differences between the two languages, I think it is no longer difficult to go from matlab to python. We will discuss some basic syntax differences between the two languages without considering the class (class.


1. The first difference is that the index start value of Python starts from 0, while matlab starts from 1, and Python has a negative index. The index with a negative number makes Python more flexible in writing programs. Because the start value starts from scratch, the index of Python falls between data rather than data. For example, if A = range (5), obtain A = [0, 1, 2, 3, 4]. At this time, the answer for A [] is [1] instead of [1, 2], which is very different from the habit of Matlab.

2. Python is a high-level literal translation language like Matlab, but Python is not designed for Matrix computing. For example, in matlab, x = [1, 2, 3], y = [, 6], z = x + y, z = [, 9], and if z = [,] is written in python, to meet the wider application characteristics of Python, The Numpy library is the most convenient way to use Python to process numeric values or matrix computing. It is processed through its array class.

3. "^" indicates the power in Matlab, but the power in Python is **. For example, the 3 power of 2 plays 2 ** 3 in Python. "^" Is not completely discarded in Python, but is used as an xor for bitwise operations, such as 2 ^ 3 = 1, because the binary of 2 is 0011, And the binary of 3 is 0010, the xor value of 0011 and 0001 is 1. In Python, such bitwise operators that can directly perform binary computation without Binary Conversion include & (and) and | (or ).

4. Common types in Matlab include integers, floating-point numbers, Boolean values, characters, strings, matrices (or arrays), and heterogeneous arrays (cells). Apart from the locations of heterogeneous arrays, most types are common in computer languages. Phthon has some unique features, so it has more flexibility in programming. First, the string column (List) in Phthon is equivalent to the heterogeneous array in Matlab. The element types in the string column do not need to stick to the same format, and the string column can be regarded as a medium expansion of the array, it is more flexible in form. Flexibility is sometimes a good thing, but excessive flexibility will make programmers accidentally create a bug that is hard to correct, therefore, in Python, a tuple type cannot be changed ). In Python, string columns are represented by brackets [...], while tuples are represented by parentheses. Whether it is a string column or a tuple, it is an ordered type, meaning that you can use an index to read the data stored in a specific location. Another special type of Python is called dictionary. The dictionary is an unordered data type with braces {...} to read the data in the dictionary, you must use the key to enable it. For example, A = {'name': 'glophy ', 'age': 18, 'url': 'http: // glophy.com '}. To read data in Dictionary A, you cannot use A {1}, A {}, because the dictionary is an unordered data type. Name, age, and url in A are called keys. The key is followed by A colon (:), followed by A colon, which is the data stored in the box of the key.

5. Python saves the end usage to indicate the end of a program block. In the first line of the program block, the ":" symbol is used to represent the beginning of the program block. How can we determine if the end program is missing? Python uses the narrative Alignment Method to Determine whether the program block is bundled (that is, the program block must be aligned by the tab key ). For example,

For I in range (5 ):

Print I

Print 'end'

Here, the for block is the same as the print of the last line, and the for block only implements the first print command.

6. In Matlab, we use the '%' symbol to represent the annotation in the program. In Python, we use '#' instead.

In addition, there are some differences in the keywords of external syntax, such as function for function Definition in matlab and def for python.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.