Välkommen till Python!

Python för MATLAB-användare

Justin Kiggins

Product Manager

Förkunskaper: Hantera matriser i MATLAB

% Example of manipulating matrices in MATLAB

v = [16 5 9 4 2 11 7 14];
disp(v(5:end))

A = magic(4);
disp(A(2:4,1:2))

A(A>12) = 10;

disp(A(:,1))
Python för MATLAB-användare

Förkunskaper: Plotta data i MATLAB

% Example of plotting data in MATLAB
figure
plot(t,y,'b-')
xlabel('Time (s)')
ylabel('Sensor A')

figure
scatter(y1,y2,'go')
xlabel('Sensor A')
ylabel('Sensor B')

figure
histogram(y1,[0:0.01:1])
Python för MATLAB-användare

Förkunskaper: Kontrollflöde i MATLAB-skript

% Example of control flow in MATLAB

fid = fopen('magic.m','r');
count = 0;
while ~feof(fid)
    line = fgetl(fid);
    if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
        continue
    end
    count = count + 1;
end
count
Python för MATLAB-användare

Om du inte kan MATLAB...

Python för MATLAB-användare

Python är mer än datavetenskap

  • Allmänt programmeringsspråk
    • Integrera maskininlärningsmodeller i storskaliga applikationer
    • Hämta data från publika API:er
    • Bygg webbplatser som klarar hög trafik
Python för MATLAB-användare

Kom igång med datatyper

  • Integer
  • Float
  • Boolean
  • String
# Integer
x = 1

print(x)
type(x)
1

<class 'int'>

 

   

 

# Float
x = 1.0  
print(x)

type(x)
1.0

<class 'float'>
Python för MATLAB-användare

Matematiska operatorer

Operation Python-operator
Addition +
Subtraktion -
Multiplikation *
Division /
Exponentiering **
a = 3 + 12
print(a)
15
b = 4 * 5.0
print(b)
20.0
Python för MATLAB-användare

$ area = \pi r^2 $

radius = 5
pi = 3.14
area = pi * (radius ** 2)
print(area)
78.5

Varning: Använd INTE caret-operatorn, ^

# This won't take 4 to the second power
print(4 ^ 2)
# This is the bitwise XOR
6
Python för MATLAB-användare

Nu kör vi en övning!

Python för MATLAB-användare

Preparing Video For Download...