Python for MATLAB Users
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 = 1
print(x)
type(x)
1
<class 'int'>
# Float x = 1.0 print(x)
type(x)
1.0
<class 'float'>
Operation | Python Operator |
---|---|
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Exponentiation | ** |
a = 3 + 12
print(a)
15
b = 4 * 5.0
print(b)
20.0
$ area = \pi r^2 $
radius = 5
pi = 3.14
area = pi * (radius ** 2)
print(area)
78.5
Warning: Do NOT use the caret operator, ^
# This won't take 4 to the second power
print(4 ^ 2)
# This is the bitwise XOR
6
Python for MATLAB Users