# Empty Arrays

An empty array in MATLAB® is an array that has no elements. Empty arrays are useful for representing the concept of "nothing" in programming. Empty arrays have specific dimensions, and at least one of those dimensions is 0. The simplest empty array is 0-by-0, but empty arrays can have some nonzero dimensions, such as 0-by-5 or 3-by-0-by-5.

### Creating Empty Arrays

To create a 0-by-0 array, use square brackets without specifying any values. Verify the array dimensions by using the `size` function.

`A = []`
```A = [] ```
`size(A)`
```ans = 1×2 0 0 ```

In most cases where you need to create an empty array, a 0-by-0 array is sufficient. However, you can create empty arrays of different sizes by using functions like `zeros` or `ones`. For example, create a 0-by-5 matrix and a 3-by-0-by-5 array.

`B = zeros(0,5)`
```B = 0x5 empty double matrix ```
`C = ones(3,0,5)`
```C = 3x0x5 empty double array ```

The square brackets, `zeros`, and `ones` create empty numeric arrays of the default `double` data type. To create empty arrays that work with text as data, use the `strings` function. This function creates a string array of any specified size with no characters. For example, create a 0-by-5 empty string array.

`S = strings(0,5)`
```S = 0x5 empty string array ```

To create empty arrays of any data type, you can use `createArray` (since R2024a). For example, create a 5-by-0 matrix of 8-bit signed integer type `int8`.

`D = createArray(5,0,"int8")`
```D = 5x0 empty int8 matrix ```

### Empty Results from Operations

Some operations can return empty arrays. These empty arrays are useful because they can flow through algorithms without requiring special-case handling.

For example, find all elements of a row vector that are less than 0 by using the `find` function. If all elements of the vector are greater than 0, then `find` returns an empty row vector of indices because no elements meet the specified condition.

```A = [1 2 3 4]; ind = find(A < 0)```
```ind = 1x0 empty double row vector ```

Indexing with the empty row vector also returns an empty row vector.

`B = A(ind)`
```B = 1x0 empty double row vector ```

Empty results can also be matrices or multidimensional arrays. For example, this code extracts a submatrix `B` from a matrix `A`, where `B` contains all nonzero rows of `A`.

```A = zeros(5,3); dim = 2; B = A(any(A,dim),:)```
```B = 0x3 empty double matrix ```

For any matrix `A`, `B` has a size of $\mathit{p}$-by-$\mathit{q}$, where $\mathit{p}$ is the number of rows of `A` that have any nonzero values, and $\mathit{q}$ is the number of columns of `A`. If no rows in `A` have nonzero values, then `B` is an empty array of size 0-by-$\mathit{q}$. Because `B` can be an empty array, you can find the number of nonzero rows in `A` without special handling for the empty case.

`n_nonzero_rows = size(B,1)`
```n_nonzero_rows = 0 ```

If you want to explicitly check for empty results, you can use the `isempty` function.

```if isempty(B) disp("All rows of A contain all zeros.") else disp("There are " + size(B,1) + " rows in A with nonzero values.") end```
```All rows of A contain all zeros. ```

### Operations on Empty Arrays

Any operation that is defined for regular matrices and arrays extends to empty arrays. Operations can involve $\mathit{m}$-by-$\mathit{n}$ arrays, even when $\mathit{m}$ or $\mathit{n}$ is zero. The sizes of the results of such operations are consistent with the sizes of the results generated when working with nonempty arrays.

Arrays with compatible sizes automatically expand to be the same size during the execution of element-wise operations. For more information, see Compatible Array Sizes for Basic Operations.

For example, the plus operator (`+`) is an element-wise operator. If `b` is a scalar, then `A + b` produces an output that is the same size as `A`, even if `A` is empty.

`A = zeros(1,0,2)`
```A = 1x0x2 empty double array ```
`C = A + 5`
```C = 1x0x2 empty double array ```

Similarly, if one input is an empty matrix `A` and the other is a column vector `B` with the same number of rows as `A`, then `A + B` produces an output that is the same size as `A`.

`A = zeros(4,0)`
```A = 4x0 empty double matrix ```
`B = ones(4,1)`
```B = 4×1 1 1 1 1 ```
`C = A + B`
```C = 4x0 empty double matrix ```

However, some functions operate vector-wise, such as the `prod` and `sum` functions. These functions produce a scalar output for a vector input or a vector output for a matrix input. These functions follow mathematical conventions when the inputs involve empty arrays.

For a nonempty input matrix, the `prod` function is a vector-wise operation on each column vector of the input matrix. For an empty input array, the `prod` function returns 1 because an empty product is the result of multiplying with no factors. By mathematical convention, the result of this product is equal to the multiplicative identity. For example, find the product of the elements in each column of nonempty and empty matrices.

`C = prod([1 2 3; 2 3 4; 3 4 5])`
```C = 1×3 6 24 60 ```
`C = prod([])`
```C = 1 ```
`C = prod(zeros(0,3))`
```C = 1×3 1 1 1 ```

### Concatenating Empty Arrays

You can use empty arrays when storing and organizing data if the data does not have specific initial values. You can dynamically grow the empty arrays by concatenating them with nonempty arrays.

When you concatenate an empty array with a nonempty array, MATLAB omits the empty array and neglects its dimensions in the output. For example, create an empty array named `data` and then expand it in a loop.

```data = []; for k = 1:3 data = [data rand(1,2)] end```
```data = 1×2 0.8147 0.9058 ```
```data = 1×4 0.8147 0.9058 0.1270 0.9134 ```
```data = 1×6 0.8147 0.9058 0.1270 0.9134 0.6324 0.0975 ```

In the first loop iteration, the empty array of size 0-by-0 is concatenated with a nonempty array of size 1-by-2, resulting in an output of size 1-by-2. Each iteration adds new data to the array.

When you concatenate two empty arrays that have compatible sizes, the result is an empty array whose size is equal to the output size as if the inputs are nonempty. For example, create an empty array of size 0-by-5. Concatenate two of these arrays horizontally and vertically to create other arrays. The results are empty arrays with sizes 0-by-10 and 0-by-5, respectively.

```A = zeros(0,5); B_horizontal = [A A]```
```B_horizontal = 0x10 empty double matrix ```
`B_vertical = [A; A]`
```B_vertical = 0x5 empty double matrix ```

### Empty Arrays as Placeholder Input Arguments

Some MATLAB functions accept empty arrays as placeholder input arguments.

For example, the `max` function finds the maximum elements of an array. If `A` is a matrix, you can use `max(A)` to find the maximum value of each column of `A`.

```A = [1 7 3; 6 2 9]; m = max(A)```
```m = 1×3 6 7 9 ```

If you specify a nonempty second input to the `max` function, it returns the largest elements from the two inputs.

```B = [5 5 5; 8 8 8]; M = max(A,B)```
```M = 2×3 5 7 5 8 8 9 ```

To find the maximum over all elements of `A`, you can pass an empty array as the second argument of `max`, and then specify `"all"`.

`m = max(A,[],"all")`
```m = 9 ```

### Removing Rows or Columns Using Square Brackets

The syntax for removing a row or column from an existing matrix also uses square brackets.

For example, remove the third column from a matrix.

`A = magic(3)`
```A = 3×3 8 1 6 3 5 7 4 9 2 ```
`A(:,3) = []`
```A = 3×2 8 1 3 5 4 9 ```

In this context, `[]` does not represent an empty array of size 0-by-0. If you assign an empty array to `A(:,3)`, such as in `A(:,3) = zeros(0,0)`, the code returns an error because the sizes of the left side and the right side of the assignment do not match.