In MATLAB, you can create an array using the following syntax:
% Create a row vector
a = [1, 2, 3, 4, 5];
% Create a matrix
b = [1 2 3; 4 5 6; 7 8 9];
In NumPy, the equivalent code would be:
import numpy as np
# Create a row vector
a = np.array([1, 2, 3, 4, 5])
# Create a matrix
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In MATLAB, indexing starts at 1. You can access elements of an array like this:
% Access the first element of a
first_element = a(1);
% Access the element in the second row and third column of b
element = b(2, 3);
In NumPy, indexing starts at 0:
# Access the first element of a
first_element = a[0]
# Access the element in the second row and third column of b
element = b[1, 2]
MATLAB provides built - in functions for mathematical operations. For example, to calculate the sum of elements in an array:
sum_a = sum(a);
In NumPy, you can use the following code:
sum_a = np.sum(a)
In MATLAB, you can design a low - pass filter and apply it to a signal as follows:
% Generate a signal
t = 0:0.01:1;
x = sin(2*pi*5*t) + sin(2*pi*20*t);
% Design a low - pass filter
[b, a] = butter(4, 0.2);
% Apply the filter
y = filter(b, a, x);
% Plot the original and filtered signals
figure;
plot(t, x, 'b', t, y, 'r');
legend('Original Signal', 'Filtered Signal');
In NumPy and SciPy, the equivalent code is:
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
# Generate a signal
t = np.arange(0, 1, 0.01)
x = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 20 * t)
# Design a low - pass filter
b, a = signal.butter(4, 0.2)
# Apply the filter
y = signal.lfilter(b, a, x)
# Plot the original and filtered signals
plt.plot(t, x, 'b', label='Original Signal')
plt.plot(t, y, 'r', label='Filtered Signal')
plt.legend()
plt.show()
In MATLAB, you can solve a system of linear equations (Ax = b):
A = [1 2; 3 4];
b = [5; 6];
x = A\b;
In NumPy, you can use the following code:
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
x = np.linalg.solve(A, b)
As mentioned earlier, the difference in indexing (1 - based in MATLAB and 0 - based in NumPy) can lead to errors. For example, if you forget to adjust the indices when porting code from MATLAB to NumPy, you may access the wrong elements.
MATLAB manages memory automatically, and you don’t need to worry much about memory allocation and deallocation. In Python, especially when dealing with large NumPy arrays, you need to be more careful. For example, creating unnecessary copies of arrays can lead to high memory usage.
# Creating an unnecessary copy
c = b.copy()
Both MATLAB and NumPy support vectorized operations, which are much faster than using loops. In NumPy, instead of using a for loop to multiply each element of an array by a scalar:
# Avoid using a loop
a = np.array([1, 2, 3])
result = []
for i in range(len(a)):
result.append(a[i] * 2)
# Use vectorized operation
result = a * 2
NumPy integrates well with other scientific libraries such as SciPy, Pandas, and Matplotlib. For example, use Pandas for data manipulation and Matplotlib for data visualization.
Transitioning from MATLAB to NumPy can be a rewarding experience for engineers. NumPy offers similar functionality to MATLAB while providing the benefits of an open - source community, a large ecosystem of libraries, and better integration with other programming languages. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, engineers can effectively make the switch and apply NumPy in real - world engineering problems.