HANDS-ON COMPUTER-AIDED PROGRAMMING FOR CHEMICAL
ENGINEERING APPLICATIONS
CHEATSHEET 2
More MATLAB Built-in Generators and Functions
1. Random Number Generation
• rand() – Uniformly distributed random numbers
• randn() – Normally distributed random numbers
• randi([imin,imax],size) – Integer random numbers in a given range
• randperm(n) – Random permutation of integers from 1 to n
• rng(seed) – Control random number generation
• sprand(m, n, density) – Sparse random matrix
• sprandn(m, n, density) – Sparse random matrix with normal distribution
2. Size, Shape, and Dimensions
• size(A) – Size of an array
• length(A) – Length of the longest dimension
• ndims(A) – Number of dimensions in an array
• numel(A) – Number of elements in an array
• reshape(A, newDims) – Reshape an array
• permute(A, order) – Rearrange dimensions of an array
• squeeze(A) – Remove singleton dimensions
• repmat(A, m, n) – Replicate and tile an array
• circshift(A, shift) – Circular shift of elements
• flip(A, dim) – Flip array along a specified dimension
• fliplr(A) – Flip left to right
• flipud(A) – Flip upside down
• rot90(A, k) – Rotate array by 90 degrees
3. Matrix and Array Creation
• zeros(m, n) – Create a matrix of zeros
• ones(m, n) – Create a matrix of ones
• eye(n) – Identity matrix
• diag(v) – Create diagonal matrix from vector
• linspace(a, b, n) – Linearly spaced vector
• logspace(a, b, n) – Logarithmically spaced vector
• meshgrid(x, y) – Generate 2D grid of points
• ndgrid(x, y, z, …) – Generate N-dimensional grid
• repelem(A, m, n) – Repeat matrix elements
• toeplitz(c, r) – Create Toeplitz matrix
• hankel(c, r) – Create Hankel matrix
• pascal(n) – Pascal matrix
4. Special Matrices
• magic(n) – Magic square matrix
• hilb(n) – Hilbert matrix
• invhilb(n) – Inverse Hilbert matrix
• hadamard(n) – Hadamard matrix
• vander(x) – Vandermonde matrix
5. Logical and Relational Operations
• logical(A) – Convert to logical array
• find(A) – Find indices of nonzero elements
• all(A) – Check if all elements are nonzero
• any(A) – Check if any elements are nonzero
• isequal(A, B) – Check if two arrays are equal
• isnan(A) – Identify NaN elements
• isinf(A) – Identify infinite elements
• isfinite(A) – Identify finite elements
• isempty(A) – Check if an array is empty
• isscalar(A) – Check if input is a scalar
• isvector(A) – Check if input is a vector
• ismatrix(A) – Check if input is a matrix
6. Set Operations
• union(A, B) – Union of two sets
• intersect(A, B) – Intersection of two sets
• setdiff(A, B) – Elements in A but not in B
• setxor(A, B) – Exclusive elements of A and B
• unique(A) – Unique elements in an array
• ismember(A, B) – Check if elements of A exist in B
7. Polynomial and Interpolation Functions
• poly(A) – Characteristic polynomial coefficients
• roots(p) – Compute roots of a polynomial
• polyval(p, x) – Evaluate a polynomial at x
• interp1(x, y, xi) – 1D interpolation
• interp2(x, y, z, xi, yi) – 2D interpolation
• spline(x, y, xi) – Cubic spline interpolation
8. Statistical Functions
• mean(A) – Mean value
• median(A) – Median value
• mode(A) – Most frequent value
• std(A) – Standard deviation
• var(A) – Variance
• cov(A) – Covariance matrix
• corrcoef(A, B) – Correlation coefficients
9. Fourier and Filtering Functions
• fft(A) – Fast Fourier Transform
• ifft(A) – Inverse Fast Fourier Transform
• fftshift(A) – Shift zero-frequency component to center
• filter(b, a, x) – 1D digital filter
• conv(A, B) – Convolution of two signals
10. File I/O and System Commands
• fopen(filename) – Open a file
• fclose(fid) – Close an open file
• fread(fid, size, precision) – Read binary data
• fwrite(fid, data, precision) – Write binary data
• dir – List directory contents
• cd(folder) – Change directory
• pwd – Print working directory
11. Execution Control and Timing
• tic – Start a stopwatch timer
• toc – Stop and read the elapsed time
• pause(seconds) – Pause execution