Things to keep in mind when using Python for those who use MATLAB

Things to keep in mind when using Python for those who use MATLAB

There was a site that wrote notes on MATLAB and Python that I use at work, so I will keep it as a memorandum.

・ 11 points to note when starting Python for those who are accustomed to MATLAB

https://datachemeng.com/matlab_to_python/

What I was addicted to when I started using Python

1. Assignment by "=" is not a copy of the value, but a reference. Unintended data changes may occur

Countermeasure: If you want to copy the value, use .copy ().
import numpy as np
# =============================================================================
# b += 5 ->The original a also changes.
# =============================================================================
a = np.array([1,2,3])
print('a = ')
print(a)
print('\n')

b = a

b += 5  #The original a also changes.
print('b = a , b +=After running 5')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')

# =============================================================================
# 'b = a , b = b + 5 ->The original a does not change.
# =============================================================================
a = np.array([1,2,3])
b = a

b = b + 5 #The original a does not change.
print('b = a ,b = b +Run 5')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')

# =============================================================================
# 'b = a , b[0] = 100 ->The original a also changes.
# =============================================================================
a = np.array([1,2,3])
b = a

b[0] = 100
print('b = a ,b[0] =After running 100')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')

# =============================================================================
# b = a.copy() b[0] = 100 ->The original a also changes.
# =============================================================================
a = np.array([1,2,3])
b = a.copy()

b[0] = 100
print('b = a.copy(),b[0] =After running 100')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')
a = 
[1 2 3]


b = a , b +=After running 5
a = 
[6 7 8]
b = 
[6 7 8]


b = a ,b = b +Run 5
a = 
[1 2 3]
b = 
[6 7 8]


b = a ,b[0] =After running 100
a = 
[100   2   3]
b = 
[100   2   3]


b = a.copy(),b[0] =After running 100
a = 
[1 2 3]
b = 
[100   2   3]

2. Array type conversion is not performed automatically. (Only when performing operations on array elements)

Countermeasure: When creating an array, do not set it with an integer, but specify it with a decimal number. Or specify the type with dtype = float.
In any case, attention to the type is essential.

Recommended Posts

Things to keep in mind when using Python for those who use MATLAB
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Things to keep in mind when copying Python lists
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
Things to keep in mind when building automation tools for the manufacturing floor in Python
Tips for those who are wondering how to use is and == in Python
Things to keep in mind when deploying Keras on your Mac
A memo for those who use Python in Visual Studio (me)
Things to keep in mind when converting row vectors to column vectors with ndarray
I tried using NVDashboard (for those who use GPU in jupyter environment)
Things to keep in mind when doing Batch Prediction on GCP ML Engine
[For beginners] How to use say command in python!
How to exit when using Python in Terminal (Mac)
For those who want to write Python with vim
Reference reference for those who want to code in Rhinoceros / Grasshopper
For those who are in trouble because NFC is read infinitely when reading NFC with Python
I know? Data analysis using Python or things you want to use when you want with numpy
Summary of points to keep in mind when writing a program that runs on Python 2.5
[For those who want to use TPU] I tried using the Tensorflow Object Detection API 2
Precautions when using pit in Python
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
When using regular expressions in Python
Things to watch out for when naming dynamic routing in nuxt.js
Python environment construction 2016 for those who aim to be data scientists
[Python] When you want to use all variables in another file
[Introduction to Python] How to use the in operator in a for statement?
Python techniques for those who want to get rid of beginners
[Short sentence] easygui for those who want to use a simple GUI with Python very easily
[Introduction to Python] How to use class in Python?
Things to watch out for when creating a Python environment on a Mac
AWS ~ For those who will use it ~
[python, multiprocessing] Behavior for exceptions when using multiprocessing
Precautions when using for statements in pandas
Log in to Slack using requests in Python
Easy way to use Wikipedia in Python
I analyzed Airbnb data for those who want to stay in Amsterdam
[Python] Organizing how to use for statements
How to use __slots__ in Python class
How to use "deque" for Python data
Notes for using python (pydev) in eclipse
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
How to use regular expressions in Python
Add words to MeCab's user dictionary on Ubuntu for use in Python
A useful note when using Python for the first time in a while
How to use is and == in Python
When I tried to use Python on WSL (windows subsystem for linux), it got stuck in Jupyter (solved)
How to take multiple arguments when doing parallel processing using multiprocessing in python
Join Azure Using Go ~ For those who want to start and know Azure with Go ~
Three things I was addicted to when using Python and MySQL with Docker
For those who want to learn Excel VBA and get started with Python
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
How to use the C library in Python
I want to use MATLAB feval with python
Dart grammar for those who only know Python
Procedure to use TeamGant's WEB API (using python)
How to use Python Image Library in python3 series