# Creating, Concatenating, and Expanding Matrices

The most basic MATLAB® data structure is the matrix. A matrix is a two-dimensional, rectangular array of data elements arranged in rows and columns. The elements can be numbers, logical values (`true` or `false`), dates and times, strings, `categorical` values, or some other MATLAB data type.

Even a single number is stored as a matrix. For example, a variable containing the value 100 is stored as a 1-by-1 matrix of type `double`.

```A = 100; whos A```
``` Name Size Bytes Class Attributes A 1x1 8 double ```

### Constructing a Matrix of Data

If you have a specific set of data, you can arrange the elements in a matrix using square brackets. A single row of data has spaces or commas in between the elements, and a semicolon separates the rows. For example, create a single row of four numeric elements. The size of the resulting matrix is 1-by-4 because it has one row and four columns. A matrix of this shape is often referred to as a row vector.

`A = [12 62 93 -8]`
```A = 1×4 12 62 93 -8 ```
`sz = size(A)`
```sz = 1×2 1 4 ```

Now create a matrix with the same numbers, but arrange them in two rows. This matrix has two rows and two columns.

`A = [12 62; 93 -8]`
```A = 2×2 12 62 93 -8 ```
`sz = size(A)`
```sz = 1×2 2 2 ```

### Specialized Matrix Functions

MATLAB has many functions that help create matrices with certain values or a particular structure. For example, the `zeros` and `ones` functions create matrices of all zeros or all ones. The first and second arguments of these functions are the number of rows and number of columns of the matrix, respectively.

`A = zeros(3,2)`
```A = 3×2 0 0 0 0 0 0 ```
`B = ones(2,4)`
```B = 2×4 1 1 1 1 1 1 1 1 ```

The `diag` function places the input elements on the diagonal of a matrix. For example, create a row vector `A` containing four elements. Then, create a 4-by-4 matrix whose diagonal elements are the elements of `A`.

```A = [12 62 93 -8]; B = diag(A)```
```B = 4×4 12 0 0 0 0 62 0 0 0 0 93 0 0 0 0 -8 ```

The `createArray` function (since R2024a) can return arrays of almost any MATLAB data type. The function uses the same syntax as `zeros` and `ones` to define the size of the array, but it also offers several options for specifying the contents of the array. For example, you can specify the fill value.

`C = createArray(2,3,FillValue=duration(1,15,0))`
```C = 2x3 duration 01:15:00 01:15:00 01:15:00 01:15:00 01:15:00 01:15:00 ```

### Concatenating Matrices

You can also use square brackets to append existing matrices. This way of creating a matrix is called concatenation. For example, concatenate two row vectors to make an even longer row vector.

```A = ones(1,4); B = zeros(1,4); C = [A B]```
```C = 1×8 1 1 1 1 0 0 0 0 ```

To arrange `A` and `B` as two rows of a matrix, use the semicolon.

`D = [A; B]`
```D = 2×4 1 1 1 1 0 0 0 0 ```

To concatenate several matrices, they must have compatible sizes. In other words, when you concatenate matrices horizontally, they must have the same number of rows. When you concatenate them vertically, they must have the same number of columns.

For example, create two matrices that both have two rows. Horizontally append the second matrix to the first by using square brackets.

`A = ones(2,3)`
```A = 2×3 1 1 1 1 1 1 ```
`B = zeros(2,2)`
```B = 2×2 0 0 0 0 ```
`C = [A B]`
```C = 2×5 1 1 1 0 0 1 1 1 0 0 ```

An alternative way to concatenate compatible matrices is to use concatenation functions, such as `horzcat`, `vertcat`, and `cat`. Horizontally append the second matrix to the first by using `horzcat`.

`D = horzcat(A,B)`
```D = 2×5 1 1 1 0 0 1 1 1 0 0 ```

### Generating a Numeric Sequence

The `colon` is a handy way to create matrices whose elements are sequential and evenly spaced. For example, create a row vector whose elements are the integers from 1 to 10.

`A = 1:10`
```A = 1×10 1 2 3 4 5 6 7 8 9 10 ```

You can use the colon operator to create a sequence of numbers within any range, incremented by one.

`A = -2.5:2.5`
```A = 1×6 -2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000 ```

To change the value of the sequence increment, specify the increment value in between the starting and ending range values, separated by colons.

`A = 0:2:10`
```A = 1×6 0 2 4 6 8 10 ```

To decrement, use a negative number.

`A = 6:-1:0`
```A = 1×7 6 5 4 3 2 1 0 ```

You can also increment by noninteger values. If an increment value does not evenly partition the specified range, MATLAB automatically ends the sequence at the last value it can reach before exceeding the range.

`A = 1:0.2:2.1`
```A = 1×6 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 ```

### Expanding a Matrix

You can add one or more elements to a matrix by placing them outside of the existing row and column index boundaries. MATLAB automatically pads the matrix with zeros to keep it rectangular. For example, create a 2-by-3 matrix and add an additional row and column to it by inserting an element in the (3,4) position.

`A = [10 20 30; 60 70 80]`
```A = 2×3 10 20 30 60 70 80 ```
`A(3,4) = 1 `
```A = 3×4 10 20 30 0 60 70 80 0 0 0 0 1 ```

You can also expand the size by inserting a new matrix outside of the existing index ranges.

`A(4:5,5:6) = [2 3; 4 5]`
```A = 5×6 10 20 30 0 0 0 60 70 80 0 0 0 0 0 0 1 0 0 0 0 0 0 2 3 0 0 0 0 4 5 ```

To expand the size of a matrix repeatedly, such as within a `for` loop, it is a best practice to preallocate space for the largest matrix you anticipate creating. Without preallocation, MATLAB has to allocate memory every time the size increases, slowing down operations. For example, preallocate a matrix that holds up to 10,000 rows and 10,000 columns by initializing its elements to zero.

`A = zeros(10000,10000);`

If you need to preallocate additional elements later, you can expand it by assigning outside of the matrix index ranges or concatenate another preallocated matrix to `A`.

### Empty Arrays

An empty array in MATLAB is an array with at least one dimension length equal to zero. Empty arrays are useful for representing the concept of "nothing" programmatically. For example, suppose you want to find all elements of a vector that are less than 0, but there are none. The `find` function returns an empty vector of indices, indicating that it did not find any elements less than 0.

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

Many algorithms contain function calls that can return empty arrays. It is often useful to allow empty arrays to flow through these algorithms as function arguments instead of handling them as a special case. If you do need to customize empty array handling, you can check for them using the `isempty` function.

`TF = isempty(ind)`
```TF = logical 1 ```