Multidimensional Arrays - MATLAB & Simulink
Multidimensional Arrays - MATLAB & Simulink
Multidimensional Arrays
• Overview
• Creating Multidimensional Arrays
• Accessing Multidimensional Array Properties
• Indexing Multidimensional Arrays
• Reshaping Multidimensional Arrays
• Permuting Array Dimensions
• Computing with Multidimensional Arrays
• Organizing Data in Multidimensional Arrays
• Multidimensional Cell Arrays
• Multidimensional Structure Arrays
Overview
An array having more than two dimensions is called a multidimensional array in the MATLAB® application.
Multidimensional arrays in MATLAB are an extension of the normal two-dimensional matrix. Matrices have two
dimensions: the row dimension and the column dimension.
You can access a two-dimensional matrix element with two subscripts: the first representing the row index, and the
second representing the column index.
Multidimensional arrays use additional subscripts for indexing. A three-dimensional array, for example, uses three
subscripts:
To access the element in the second row, third column of page 2, for example, you use the subscripts (2,3,2).
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 1/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
As you add dimensions to an array, you also add subscripts. A four-dimensional array, for example, has four subscripts.
The first two reference a row-column pair; the second two access the third and fourth dimensions of data.
Most of the operations that you can perform on matrices (i.e., two-dimensional arrays) can also be done on
multidimensional arrays.
Note
The general multidimensional array functions reside in the datatypes directory.
A = [5 7 8; 0 1 9; 4 3 6];
A(:,:,2) = [1 0 4; 3 5 6; 9 8 7]
A(:,:,1) =
5 7 8
0 1 9
4 3 6
A(:,:,2) =
1 0 4
3 5 6
9 8 7
You can extend an array by assigning a single value to the new elements. MATLAB expands the scalar value to match
the dimensions of the addressed elements. This expansion is called scalar expansion.
A(:,:,3) = 5
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 2/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
A(:,:,1) =
5 7 8
0 1 9
4 3 6
A(:,:,2) =
1 0 4
3 5 6
9 8 7
A(:,:,3) =
5 5 5
5 5 5
5 5 5
To extend the rows, columns, or pages of an array, use similar assignment statements. The dimensions of arrays on the
right side and the left side of the assignment must be the same.
Extend A into a 3-by-3-by-3-by-2, four-dimensional array. In the first assignment, MATLAB pads A to fill the unassigned
elements in the extended dimension with zeros. The second and third assignments replace the zeros with the specified
values.
A(:,:,1,2) = [1 2 3; 4 5 6; 7 8 9];
A(:,:,2,2) = [9 8 7; 6 5 4; 3 2 1];
A(:,:,3,2) = [1 0 1; 1 1 0; 0 1 1];
B = randn(4,3,2)
To generate an array filled with a single constant value, use the repmat function. repmat replicates an array (in this
case, a 1-by-1 array) through a vector of array dimensions.
B = repmat(5, [3 4 2])
B(:,:,1) =
5 5 5 5
5 5 5 5
5 5 5 5
B(:,:,2) =
5 5 5 5
5 5 5 5
5 5 5 5
Note
Any dimension of an array can have size zero, making it a form of empty array. For example, 10-by-0-by-20 is
a valid size for a multidimensional array.
where A1, A2, and so on are the arrays to concatenate, and dim is the dimension along which to concatenate the
arrays.
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 3/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
B(:,:,1) =
2 8
0 5
B(:,:,2) =
1 3
7 9
The cat function accepts any combination of existing and new data. In addition, you can nest calls to cat. The lines
below, for example, create a four-dimensional array.
cat automatically adds subscripts of 1 between dimensions, if necessary. For example, to create a 2-by-2-by-1-by-2
array, enter
In the previous case, cat inserts as many singleton dimensions as needed to create a four-dimensional array whose
last dimension is not a singleton dimension. If the dim argument had been 5, the previous statement would have
produced a 2-by-2-by-1-by-1-by-2 array. This adds additional 1s to indexing expressions for the array. To access the
value 8 in the four-dimensional case, use
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 4/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
To access a single element of a multidimensional array, use integer subscripts. Each subscript indexes a dimension—
the first indexes the row dimension, the second indexes the column dimension, the third indexes the first page
dimension, and so on.
You can use vectors as array subscripts. In this case, each vector element must be a valid subscript, that is, within the
bounds defined by the dimensions of the array. To access elements (2,1), (2,3), and (2,4) on page 3 of nddata,
use
nddata(2,[1 3 4],3);
The colon operator is also useful for accessing other subsets of data. For example, nddata(2:3,2:3,1) results in a 2-
by-2 array, a subset of the data on page 1 of nddata. This matrix consists of the data in rows 2 and 3, columns 2 and 3,
on the first page of the array.
The colon operator can appear as an array subscript on both sides of an assignment statement. For example, to create
a 4-by-4 array of zeros:
C = zeros(4, 4)
Now assign a 2-by-2 subset of array nddata to the four elements in the center of C.
C(2:3,2:3) = nddata(2:3,1:2,2)
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 5/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
Again, a single subscript indexes directly into this column. For example, C(4) produces the result
ans =
0
If you specify two subscripts (i,j) indicating row-column indices, MATLAB calculates the offset as described above.
Two subscripts always access the first page of a multidimensional array, provided they are within the range of the
original array dimensions.
If more than one subscript is present, all subscripts must conform to the original array dimensions. For example,
C(6,2) is invalid because all pages of C have only five rows.
If you specify more than two subscripts, MATLAB extends its indexing scheme accordingly. For example, consider four
subscripts (i,j,k,l) into a four-dimensional array with size [d1 d2 d3 d4]. MATLAB calculates the offset into the
storage column by
(l-1)(d3)(d2)(d1)+(k-1)(d2)(d1)+(j-1)(d1)+i
For example, if you index the array C using subscripts (3, 4, 2, 1), MATLAB returns the value 5 (index 38 in the storage
column).
In general, the offset formula for an array with dimensions [d1 d2 d3 ... dn] using any subscripts (s1 s2 s3 ...
sn) is
(sn-1)(dn-1)(dn-2)...(d1)+(sn-1-1)(dn-2)...(d1)+...+(s2-1)(d1)+s1
Because of this scheme, you can index an array using any number of subscripts. You can append any number of 1s to
the subscript list because these terms become zero. For example,
C(3,2,1,1,1,1,1,1)
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 6/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
is equivalent to
C(3,2)
A(:,:,2) = 1:10
are ambiguous because they do not provide enough information about the shape of the dimension to receive the data.
In the case above, the statement tries to assign a one-dimensional vector to a two-dimensional destination. MATLAB
produces an error for such cases. To resolve the ambiguity, be sure you provide enough information about the
destination for the assigned data, and that both data and destination have the same shape. For example:
A(1,:,2) = 1:10;
B = reshape(A,[s1 s2 s3 ...])
s1, s2, and so on represent the desired size for each dimension of the reshaped matrix. Note that a reshaped array
must have the same number of elements as the original array (that is, the product of the dimension sizes is constant).
M reshape(M, [6 5])
The reshape function operates in a columnwise manner. It creates the reshaped matrix by taking consecutive elements
down each column of the original data construct.
C reshape(C, [6 2])
B = reshape(nddata, [6 25])
C = reshape(nddata, [5 3 10])
D = reshape(nddata, [5 3 2 5])
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 7/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
A = eye(2)
A =
1 0
0 1
size(A,4)
ans =
Although A is a 2-by-2 matrix, the size of the fourth dimension in A is 1. In fact, the size of each dimension beyond the
second is 1.
The first two dimensions of an array are never stripped away, since they are always relevant.
size(3)
ans =
1 1
MATLAB creates singleton dimensions if you explicitly specify them when you create or reshape an array, or if you
perform a calculation that results in an array dimension of one:
B = repmat(5, [2 3 1 4]);
size(B)
ans =
2 3 1 4
C = squeeze(B);
size(C)
ans =
2 3 4
The squeeze function does not affect two-dimensional arrays; row vectors remain rows.
B = permute(A, dims);
dims is a vector specifying the new order for the dimensions of A, where 1 corresponds to the first dimension (rows), 2
corresponds to the second dimension (columns), 3 corresponds to pages, and so on.
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 8/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
For a more detailed look at the permute function, consider a four-dimensional array A of size 5-by-4-by-3-by-2.
Rearrange the dimensions, placing the column dimension first, followed by the second page dimension, the first page
dimension, then the row dimension. The result is a 4-by-2-by-3-by-5 array.
You can think of permute's operation as an extension of the transpose function, which switches the row and column
dimensions of a matrix. For permute, the order of the input dimension list determines the reordering of the subscripts.
In the example above, element (4,2,1,2) of A becomes element (2,2,1,4) of B, element (5,4,3,2) of A becomes
element (4,2,3,5) of B, and so on.
Inverse Permutation
The ipermute function is the inverse of permute. Given an input array A and a vector of dimensions v, ipermute
produces an array B such that permute(B,v) returns A.
For example, these statements create an array E that is equal to the input array C:
D = ipermute(C, [1 4 2 3]);
E = permute(D, [1 4 2 3])
You can obtain the original array after permuting it by calling ipermute with the same vector of dimensions.
Operating on Vectors
Functions that operate on vectors, like sum, mean, and so on, by default typically work on the first nonsingleton
dimension of a multidimensional array. Most of these functions optionally let you specify a particular dimension on
which to operate. There are exceptions, however. For example, the cross function, which finds the cross product of two
vectors, works on the first nonsingleton dimension having length 3.
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 9/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
Note
In many cases, these functions have other restrictions on the input arguments — for example, some functions
that accept multiple arrays require that the arrays be the same size. Refer to the online help for details on
function arguments.
Operating Element-by-Element
MATLAB functions that operate element-by-element on two-dimensional arrays, like the trigonometric and exponential
functions in the elfun directory, work in exactly the same way for multidimensional cases. For example, the sin
function returns an array the same size as the function's input argument. Each element of the output array is the sine of
the corresponding element of the input array.
Similarly, the arithmetic, logical, and relational operators all work with corresponding elements of multidimensional
arrays that are the same size in every dimension. If one operand is a scalar and one an array, the operator applies the
scalar to each element of the array.
You can use indexing to apply a matrix function or operator to matrices within a multidimensional array. For example,
create a three-dimensional array A:
Applying the eig function to the entire multidimensional array results in an error:
eig(A)
??? Undefined function or method 'eig' for input
arguments of type 'double' and attributes 'full 3d real'.
You can, however, apply eig to planes within the array. For example, use colon notation to index just one page (in this
case, the second) of the array:
eig(A(:,:,2))
ans =
12.9129
-2.6260
2.7131
Note
In the first case, subscripts are not colons; you must use squeeze to avoid an error. For example,
eig(A(2,:,:)) results in an error because the size of the input is [1 3 3]. The expression
eig(squeeze(A(2,:,:))), however, passes a valid two-dimensional matrix to eig.
• As planes or pages of two-dimensional data. You can then treat these pages as matrices.
• As multivariate or multidimensional data. For example, you might have a four-dimensional array where each
element corresponds to either a temperature or air pressure measurement taken at one of a set of equally spaced
points in a room.
For example, consider an RGB image. For a single image, a multidimensional array is probably the easiest way to store
and access data.
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 10/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
redPlane = RGB(:,:,1);
subimage = RGB(20:40,50:85,:);
The RGB image is a good example of data that needs to be accessed in planes for operations like display or filtering. In
other instances, however, the data itself might be multidimensional. For example, consider a set of temperature
measurements taken at equally spaced points in a room. Here the location of each value is an integral part of the data
set—the physical placement in three-space of each element is an aspect of the information. Such data also lends itself
to representation as a multidimensional array.
mean(mean(mean(TEMP)));
To obtain a vector of the “middle” values (element (2,2)) in the room on each page, use
B = TEMP(2,2,:);
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 11/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 12/13
14/03/2018 Multidimensional Arrays - MATLAB & Simulink
To apply functions to multidimensional structure arrays, operate on fields and field elements using indexing. For
example, find the sum of the columns of the test array in patient(1,1,2):
sum((patient(1,1,2).test));
total = sum([patient.billing]);
https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html 13/13