Python voor MATLAB-gebruikers
Justin Kiggins
Product Manager
% 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))
% 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])
% 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
# Integer x = 1print(x)type(x)
1<class 'int'>
# Float x = 1.0 print(x)type(x)
1.0<class 'float'>
| Bewerking | Python-operator |
|---|---|
| Optellen | + |
| Aftrekken | - |
| Vermenigvuldigen | * |
| Delen | / |
| Machtsverheffen | ** |
a = 3 + 12
print(a)
15
b = 4 * 5.0
print(b)
20.0
$ oppervlakte = \pi r^2 $
radius = 5
pi = 3.14
area = pi * (radius ** 2)
print(area)
78.5
Waarschuwing: Gebruik de dakje-operator ^ NIET
# Dit verheft 4 niet tot de tweede macht
print(4 ^ 2)
# Dit is de bitwise XOR
6
Python voor MATLAB-gebruikers