Main Content

Arrays with Dimensions of Size 1

An array has multiple dimensions, and the size of each dimension indicates the number of elements are along that dimension. Arrays can include dimensions of size 1 (also known as a singleton dimension).

  • In MATLAB®, every array can have any number of trailing dimensions of size 1. In other words, a 3-by-4 matrix is the same as an array of size 3-by-4-by-1, 3-by-4-by-1-by-1, and so on. However, MATLAB does not display trailing dimensions of size 1 beyond the second dimension.

  • Dimensions of size 1 do not contribute to the actual volume of the array, but they can be important for maintaining the shape and structure of the array during certain operations, such as concatenation.

Trailing Dimensions of Size 1

To see how MATLAB treats trailing dimensions of size 1, first create a 2-by-1-by-2-by-1-by-1 array A using the rand function, and then compute its size. MATLAB displays the array as if its size is 2-by-1-by-2, ignoring the trailing dimensions of size 1.

A = rand(2,1,2,1,1)
A = 
A(:,:,1) =

    0.8147
    0.9058


A(:,:,2) =

    0.1270
    0.9134

sz = size(A)
sz = 1×3

     2     1     2

Check the number of dimensions of A by using ndims. The result is three dimensions.

nd = ndims(A)
nd = 
3

Even though MATLAB displays the size of A as 2-by-1-by-2, you can still index into the trailing dimensions of size 1 and query the size of A beyond the third dimension.

B = A(:,:,:,1,1)
B = 
B(:,:,1) =

    0.8147
    0.9058


B(:,:,2) =

    0.1270
    0.9134

sz = size(A,1:5)
sz = 1×5

     2     1     2     1     1

Concatenate Arrays with Dimensions of Size 1

In MATLAB, concatenation is a fundamental operation for combining arrays. When you work with multidimensional arrays, dimensions of size 1 are important in how MATLAB manipulates and combines these arrays.

For example, consider three arrays that represent three different color channels on a 1920-by-1080 grid. The arrays have different dimensions, but each array has one dimension of size 1:

  • 1920-by-1080-by-1 for the first color channel

  • 1920-by-1-by-1080 for the second color channel

  • 1-by-1920-by-1080 for the third color channel

c1xy = randi([0 255],1920,1080,1);
c2xz = randi([0 255],1920,1,1080);
c3yz = randi([0 255],1,1920,1080);

Concatenate these three color channels to create a single image array of size 1920-by-1080-by-3. Use the squeeze function to remove the dimensions of size 1 from each array. Then, concatenate the arrays along the third dimension using the cat function.

imgA = cat(3,squeeze(c1xy),squeeze(c2xz),squeeze(c3yz));
sz = size(imgA)
sz = 1×3

        1920        1080           3

Consider another array with size 1920-by-1080-by-3 that represents another image, where the image also has three color channels. You can append this image to the previous image along the fourth dimension. The result is an array of size 1920-by-1080-by-3-by-2.

imgComb = randi([0 255],1920,1080,3);
imgComb = cat(4,imgComb,imgA);
sz = size(imgComb)
sz = 1×4

        1920        1080           3           2

If you want to replace the third image in the collection of two images with another image, use indexed assignment.

imgB = randi([0 255],1920,1080,3);
imgComb(:,:,:,2) = imgB;

Next, concatenate the initial three color channels into a single image array of size 3-by-1920-by-1080 instead. Use the reshape function to rearrange the three arrays to have matching sizes. Reshape the arrays c1xy and c2xz into 1-by-1920-by-1080 arrays, and concatenate them with the array c3yz that already has the size of 1-by-1920-by-1080.

c1other = reshape(c1xy,1,1920,1080);
c2other = reshape(c2xz,1,1920,1080);
imgC = cat(1,c1other,c2other,c3yz);
sz = size(imgC)
sz = 1×3

           3        1920        1080

See Also

Topics