MATLAB - Compatible Arrays
MATLAB - Implicit Expansion
Section titled “MATLAB - Implicit Expansion”Implicit expansion, sometimes known as broadcasting, is a powerful MATLAB feature that simplifies vector and matrix operations. When you perform element-wise operations on arrays of different but compatible sizes, MATLAB automatically expands the smaller dimensions to match the larger ones, avoiding the need for manual replication of arrays.
This feature was formally expanded and named in MATLAB R2016b. Two arrays are compatible for an operation if, for every dimension, the dimension sizes are either equal or one of them is 1.
Core Principles of Implicit Expansion
Section titled “Core Principles of Implicit Expansion”- Scalar Expansion: The most common case. A scalar can be combined with any size array. The scalar is effectively expanded to match the size of the other array.
- Vector and Matrix Expansion: A row or column vector can be combined with a 2D matrix if its length matches the corresponding matrix dimension (row vector length matches number of columns, column vector length matches number of rows).
Example 1: Scalar Expansion
Section titled “Example 1: Scalar Expansion”Adding a scalar to a matrix. MATLAB expands the scalar to a matrix of the same size and then performs the addition.
matrix = [1, 2; 3, 4];scalar = 10;
% Implicit expansion of 'scalar' to a 2x2 matrixresult = matrix + scalar;
disp('Original Matrix:');disp(matrix);disp('Result after adding a scalar:');disp(result);The output demonstrates the element-wise addition:
Original Matrix: 1 2 3 4
Result after adding a scalar: 11 12 13 14Example 2: Vector and Matrix Expansion
Section titled “Example 2: Vector and Matrix Expansion”Subtracting a column vector from a matrix. This is a common operation for normalizing data, such as subtracting the mean of each row.
% A 3x4 matrix of sample datadata = [10, 20, 30, 40; ... 15, 25, 35, 45; ... 5, 10, 15, 20];
% A 3x1 column vector of row meansrowMeans = mean(data, 2); % The '2' indicates mean across columns (i.e., for each row)
% Subtract the mean from each element in its corresponding row.% The 3x1 'rowMeans' vector is implicitly expanded to a 3x4 matrix.centeredData = data - rowMeans;
disp('Original Data:');disp(data);disp('Row Means (3x1 vector):');disp(rowMeans);disp('Centered Data (after implicit expansion):');disp(centeredData);The output shows how the column vector was subtracted from each column of the matrix:
Original Data: 10 20 30 40 15 25 35 45 5 10 15 20
Row Means (3x1 vector): 25.0000 30.0000 12.5000
Centered Data (after implicit expansion): -15.0000 -5.0000 5.0000 15.0000 -15.0000 -5.0000 5.0000 15.0000 -7.5000 -2.5000 2.5000 7.5000Example 3: Row and Column Vector Expansion
Section titled “Example 3: Row and Column Vector Expansion”When you combine a column vector (size M-by-1) and a row vector (size 1-by-N), the result is an M-by-N matrix. This is a powerful way to generate grids or tables of values.
colVector = [10; 20; 30]; % 3x1 vectorrowVector = [1, 2, 3, 4]; % 1x4 vector
% The column vector is expanded across 4 columns% The row vector is expanded down 3 rowsresult = colVector + rowVector;
disp('Result of adding a 3x1 and 1x4 vector:');disp(result);The resulting 3x4 matrix is:
Result of adding a 3x1 and 1x4 vector: 11 12 13 14 21 22 23 24 31 32 33 34Common Errors: Dimension Mismatch
Section titled “Common Errors: Dimension Mismatch”An error occurs if array dimensions are not compatible. For an operation to work, each dimension must either be the same size, or one of them must be 1.
% Incompatible arraysA = [1, 2, 3]; % size 1x3B = [4; 5]; % size 2x1
try C = A + B; % This will failcatch ME disp('Error caught as expected:'); disp(ME.message);endThis code will produce an error because the dimensions (1x3 and 2x1) are not compatible:
Error caught as expected:Error using +Matrix dimensions must agree.