Thursday, June 16, 2016

MATLAB vs Python Syntax (II)

MATLAB vs Python Syntax (II)
MATLAB Python (Numpy)
Matrix Summary
sum(A) #veritical 1xn A.sum(0) #1D array(n)
sum(A,2) #horizontal mx1 A.sum(1) #1D array(m)
sum(sum(A)) #total A.sum()
max(A) #1xn A.max(0) #(n)
max(A, [], 2) #mx1 A.max(1) #(m)
max(max(A)) A.max()
Shuffle Data
X(randperm(m), :) np.random.permutation(A)
Plot Graph
- import matplotlib.pyplot as plt
fugure(1) plt.figure(1)
r=randn(5000,1) r=np.randn(5000)
hist(r, 100) plt.hist(r, 100)
figure(2) plt.figure(2)
t=[0: 0.01: 0.98] t=arange(0, 0.99, 0.01)
y1=sin(2*pi*t) y1=np.sin(2*np.pi*t)
plot(t, y1) plt.plot(t, y1)
hold no #no need plt.clf() #clear
plot(t, y2, 'r') plt.plot(t, y2, 'r')
xlabel('time') plt.xlabel('time')
ylabel('value') plt.ylabel('value')
legend('sin', 'cos') plt.legend(('sin', 'cos'))
title('my plot') plt.tittle('my plot')
close all plt.close('all')
Flow Control
v=zero(10, 1) v=np.zeros(10)
for i=1:2:10, #skip 2 for i in range(0,m,2):
..v(i) = 2^i; ..v[i] = 2 ** i
end;
i=1; i=0
while i<=5, while i<5:
..v(i) = 100; ..v[i] = 100
..i = i+1; ..i = i+1
end;
v(1)=2; v[0]=2
if v(1)==1, if v[0]==1:
..disp('The value is one'); ..print 'The value is one'
elseif v(1)==2, elif v[0]==2:
..disp('The value is two'); ..print 'The value is two'
else, else:
..disp('The value is others'); ..print 'The value is others'
end;

No comments:

Post a Comment

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...