MATLAB | Python (Numpy) |
---|---|
I/O | |
load 'hello.txt' X |
X=pd.read_csv('hello.txt, header=None) |
save 'hello.txt' X -ascii |
np.savetxt('hello.txt', X) |
- | import scipy.io as sio |
- | test=sio.loadmat('test.mat') |
Data Creation | |
A=[1 2; 3 4; 5 6] |
A=np.array([[1,2], [3,4], [5,6]]) |
v=[1 2 3] |
v=np.array([1, 2, 3]) |
v=[1; 2; 3] |
v=np.array([[1], [2], [3]]) |
v=1: 0.1: 2 |
v=np.arange(1, 2.1, 0.1) |
c=2*ones(2,3) |
v=2.*np.ones((2, 3)) |
w=zeros(1,3) |
w=np.zeros(3).T |
r=rand(1,3) |
r=np.random.rand(3,4) |
I=eye(4) |
I=np.eye(4) |
size(A) #3 2 |
A.shape |
length(v) #3 |
v.size |
m=size(A, 1) |
m=A.shape[0] |
Data Extraction | |
A(3, 2) #6 |
A[2, 1] |
A(2,:) |
A[1] |
A([1 3], :) |
A[[0, 2]] |
A(2:end, 1) |
A[1:, 0] |
R=rand(4,5) |
R=np.random.rand(4,5) |
R(R(:,3)>0.5, [2,4]) |
R[R[:2]>0.5][:,[1,3]] |
pos=find(p>0.5) |
pos=np.where(p>0.5) |
X1=X(pos, :) |
X1=X[pos] |
X1=X(p>0.5, :) |
X1=X[p>0.5, :] |
Concatenate Data | |
A=[A, [101; 100; 102]] |
A=np.hstack([A, np.array([[101], [102], [103]]) ]) |
- | A=np.c_[A, np.array([[101], [102], [103]])] |
X=[ones(m,1), X] |
np.c_[np.ones(m), X] |
Basic Operation | |
a == b |
a == b |
a ~= b |
a != b |
a && b |
a and b |
a \|\| b |
a or b |
xor(a,b) |
a ^ b |
2^3 |
2**3 |
Matrix Operation | |
A * B #dot product |
A.dot(B) |
A' #transpose |
A.T |
A' + B |
A.T + B |
A .* B #element-wise |
A * B |
A .^ 2 |
A ** 2 |
1 ./ A |
1. / A |
log(A) |
np.log(A) |
exp(A) |
np.exp(A) |
A * v #result mx1 |
A.dot(v) #result 1D array |
pinv(A) #inverse |
np.linalg.pinv(A) |
Friday, June 10, 2016
MATLAB vs Python Syntax (I)
Subscribe to:
Post Comments (Atom)
Principle Component Analysis
Principle Component Analysis Eigenvector Decomposition Let A ∈ R n × n A \in \R^{n \times n} A ∈ R n × n be an n by n...
-
Policy Based Reinforcement Learning (I) Pre-requisites Unsupervised Learning Value Based Reinforcement Learning Ba...
No comments:
Post a Comment