MATLAB - Multi-Dimensional Array
MATLAB - Multi-dimensional Arrays
Section titled “MATLAB - Multi-dimensional Arrays”While vectors (1-D) and matrices (2-D) are the most common arrays in MATLAB, the language natively supports arrays with any number of dimensions. A multi-dimensional (or N-D) array is a powerful way to store and organize complex data. You can think of a 3-D array as a book of spreadsheets, where each page is a 2-D matrix.
Practical Use Cases
Section titled “Practical Use Cases”Multi-dimensional arrays are essential for many real-world applications:
- Image Processing: A standard color (RGB) image is an
M-by-N-by-3array, where M is the height, N is the width, and the 3 “pages” represent the Red, Green, and Blue color channels. - Time-Series Analysis: Storing data from multiple sensors over time can be represented as a
num_samples-by-num_sensors-by-num_trialsarray. - Numerical Simulation: Storing the results of a parameter sweep, like the temperature on a plate over time, could be an
x_position-by-y_position-by_time_steparray.
Creating Multi-dimensional Arrays
Section titled “Creating Multi-dimensional Arrays”You can create N-D arrays using initialization functions or by concatenating existing arrays.
% 1. Using initialization functions (recommended)% Create a 2x4x3 array of random numbersA = rand(2, 4, 3);
% Create a 5x2x4 array of all zerosZ = zeros(5, 2, 4);
% 2. Concatenating existing matrices% The cat() function is the robust way to build N-D arraysmat1 = [1 2; 3 4]; % 2x2 matrixmat2 = [5 6; 7 8]; % Another 2x2 matrix
% Concatenate along the 3rd dimension (pages) to create a 2x2x2 arrayC = cat(3, mat1, mat2);The size() function works for N-D arrays, returning the length of each dimension.
size(C)ans = 2 2 2Accessing Elements
Section titled “Accessing Elements”To access elements, you provide an index for each dimension, separated by commas. The colon : operator selects all elements in a dimension, and the end keyword refers to the last element.
% Let's use the 2x2x2 array C from the previous example% C(:,:,1) is [1 2; 3 4], C(:,:,2) is [5 6; 7 8]
% Get the element in row 1, col 2, page 2elem = C(1, 2, 2);% Result: 6
% Get the entire first row from the first pagerow_vec = C(1, :, 1);% Result: [1 2]
% Get the entire second column from all pages% This results in a 2x1x2 arraycol_matrix = C(:, 2, :);
% Use squeeze() to remove singleton dimensions (like the middle '1')squeezed_cols = squeeze(col_matrix);% Result: a 2x2 matrix [2 6; 4 8]Computation with Multi-dimensional Arrays
Section titled “Computation with Multi-dimensional Arrays”Most MATLAB mathematical functions can operate along a specified dimension of an N-D array. This is extremely powerful for data analysis.
% Create a 3x4x2 sample arraydata = randi(10, [3, 4, 2]);
% Find the mean of each column (i.e., operate along dimension 1)mean_cols = mean(data, 1);% Result will be a 1x4x2 array
% Find the sum of each page (i.e., operate along dimension 3)sum_pages = sum(data, 3);% Result will be a 3x4 matrixManipulating Array Dimensions
Section titled “Manipulating Array Dimensions”Sometimes you need to rearrange the dimensions of an array for computation or interfacing with other tools. MATLAB provides several key functions for this.
reshape(A, sz): Changes the size of an array without changing the order of its elements in memory. The total number of elements must remain the same.permute(A, order): Rearranges the dimensions of the array. This is useful for tasks like transposing an RGB image fromMxNx3to3xMxN.squeeze(A): Removes dimensions of length 1. Useful for simplifying arrays after slicing.pagetranspose(A)orA.': Transposes each 2-D page of a multi-dimensional array (new in R2020b).
Example: Permuting Image Channels
Section titled “Example: Permuting Image Channels”% Create a pseudo-image: 100 pixels high, 80 pixels wide, 3 color channelsrgbImage = rand(100, 80, 3);
% Some libraries might expect the channel dimension first (channel x height x width)% The original order is [1, 2, 3] for (height, width, channel)% We want the new order to be [3, 1, 2]permutedImage = permute(rgbImage, [3, 1, 2]);
size(rgbImage)ans = 100 80 3
size(permutedImage)ans = 3 100 80