MATLAB
Matrix Creation and Operations
Create and operate on matrices in MATLAB.
By EZ4Code Team
matrixlinear-algebra
Code
% Create matrices
A = [1 2 3; 4 5 6; 7 8 9];
B = eye(3); % 3x3 identity
C = zeros(2, 4); % 2x4 zeros
D = rand(3, 3); % 3x3 uniform random
% Element-wise operations (use .)
A .* A % element-wise square
A .^ 2 % same
A ./ A % element-wise divide
% Matrix operations
A' % transpose
inv(A) % inverse
det(A) % determinant
rank(A) % rank
eig(A) % eigenvalues
A \ b % solve Ax = b (left divide)Explanation
MATLAB (MATrix LABoratory) treats everything as a matrix. Element-wise operators use a dot (.* ./ .^), while matrix operators don't. The backslash operator (\) solves linear systems efficiently and is preferred over inv(A)*b for numerical stability.
More MATLAB Snippets
2D Plotting
Create line plots with labels, legends, and styling.
Functions and Scripts
Define functions in separate files or at end of scripts.
Cell Arrays and Structs
Heterogeneous data containers in MATLAB.
File I/O
Read and write .mat, .csv, and text files.
ODE Solvers
Solve ordinary differential equations with ode45.
Signal Processing (FFT)
Compute and visualize the FFT of a signal.