zaro

How do you create a multi dimensional array in MATLAB?

Published in MATLAB multidimensional array 4 mins read

You create a multidimensional array in MATLAB by starting with a 2-D matrix and then extending it by assigning values to indices in higher dimensions.

In MATLAB, a multidimensional array is essentially an extension of a 2-D matrix into three or more dimensions. Unlike some other programming languages, you don't strictly "declare" the size of the higher dimensions upfront in the same way you might for a simple 2D array. Instead, MATLAB dynamically expands the array as you assign values to indices beyond its current size in any dimension.

Understanding MATLAB Array Dimensions

Before creating a multidimensional array, it's helpful to understand how MATLAB views dimensions:

  • Dimension 1: Rows
  • Dimension 2: Columns
  • Dimension 3: Pages (often visualized as stacked 2D matrices)
  • Dimension 4 and beyond: Can be interpreted in various ways depending on the data (e.g., time steps, different variables, experimental trials).

Creating a Multidimensional Array: The Extension Method

As highlighted in the reference, a common and intuitive way to build a multidimensional array is by first creating a standard 2-D matrix and then extending it.

Step-by-Step Process

  1. Start with a 2-D Matrix: Create a matrix (rows x columns) which will serve as the first "page" or "slice" of your multidimensional array.
  2. Assign to a Higher Dimension Index: To add more "pages" or extend into further dimensions, assign data (which must be compatible in size for the existing dimensions) to an index in a dimension higher than the current maximum dimension of the array.

MATLAB automatically fills any unassigned indices up to the specified size with zeros.

Practical Example

Let's create a 3-D array representing two 3x3 matrices (two "pages").

% Step 1: Create the first 3x3 matrix (the first page)
matrix1 = [1 2 3; 4 5 6; 7 8 9];

% Assign this 3x3 matrix to the first page (index 1) in the third dimension
% The syntax is Array(rows, columns, page_index)
myMultiArray(:,:,1) = matrix1;

% At this point, myMultiArray is a 3x3x1 array (effectively still 2D)

% Step 2: Create a second 3x3 matrix for the second page
matrix2 = [10 11 12; 13 14 15; 16 17 18];

% Assign this second 3x3 matrix to the second page (index 2) in the third dimension
myMultiArray(:,:,2) = matrix2;

% Now, myMultiArray is a 3x3x2 array.
% MATLAB automatically expanded it to accommodate the assignment.

% You can add a third page, if needed
matrix3 = rand(3,3); % Create another 3x3 matrix
myMultiArray(:,:,3) = matrix3; % Assign it to the third page

% myMultiArray is now a 3x3x3 array.

This method clearly shows how you can create a 2-D matrix first, and then extend it by assigning subsequent 2-D matrices to indices in the third (or higher) dimension, just as described in the reference.

Accessing Elements

You access elements using the same index notation: Array(row_index, col_index, page_index, ...)

% Get the element in the 2nd row, 1st column of the first page
element1 = myMultiArray(2, 1, 1); % element1 will be 4

% Get the element in the 3rd row, 3rd column of the second page
element2 = myMultiArray(3, 3, 2); % element2 will be 18

% Get the entire first page
page1 = myMultiArray(:,:,1);

% Get the entire second column across all pages
secondColumn = myMultiArray(:,2,:);

Alternative Method: Pre-allocating with zeros

While the extension method is flexible, for performance reasons, especially with large arrays, it's often better to pre-allocate the array using functions like zeros, ones, rand, or NaN before filling it with data.

% Pre-allocate a 3x4x5 multidimensional array filled with zeros
preAllocatedArray = zeros(3, 4, 5);

% Now you can assign values to specific indices or slices
preAllocatedArray(:,:,1) = [1 2 3 4; 5 6 7 8; 9 10 11 12];
preAllocatedArray(:,:,2) = ones(3,4);
% ... and so on for pages 3, 4, and 5

This pre-allocation method defines the dimensions explicitly from the start, which can be more efficient than letting MATLAB expand the array dynamically many times. However, the core concept of working with dimensions (rows, columns, pages, etc.) remains the same.

Summary Table: Key Dimensions

Dimension Standard Interpretation MATLAB Syntax (Accessing)
1 Rows Array(row_idx, ...)
2 Columns Array(:, col_idx, ...)
3 Pages/Planes Array(:,:, page_idx, ...)
4+ Higher Dimensions Array(:,:,:, dim4_idx, ...)

Creating a multidimensional array in MATLAB is straightforward, primarily relying on extending a 2-D structure by assigning data to indices in higher dimensions, or by pre-allocating the required size directly.