## Vector/Matrix Constructors

### Brackets

The simplest way to construct a small vector or matrix is to use brackets:

```>> [1 2 3 5]                     % a row vector (actually, a 1x4 matrix)

ans =

1     2     3     5

>> [1, 2, 3, 5]                  % a row vector using commas

ans =

1     2     3     5

>> [1; 2; 3; 5]                  % a column vector (actually, a 4x1 matrix)

ans =

1
2
3
5

>> [1 2 3 4; 2 4 6 8; 3 6 9 12]  % a 3x4 matrix using semicolons

ans =

1     2     3     4
2     4     6     8
3     6     9    12

>> [1 2 3 4                      % a 3x4 matrix using Enter
2 4 6 8
3 6 9 12]

ans =

1     2     3     4
2     4     6     8
3     6     9    12

>> 3                             % a scalar (actually, a 1x1 matrix)

ans =

3

```

A space or comma separates entries within a column, and a semicolon or Enter separates rows.

A few things to note:

1. As you can see, everything in Matlab is treated as a matrix, including scalars. This is useful to keep in the back of your mind as you use the product.
2. When calling any of these functions with one or more arguments, you may either call them in the form f(a, b, c), or you may give a single row vector, for example, f([a b c]).
3. With a number of constructors, if only one dimension is provided, it is assumed you want a square matrix with that dimension. For example, ones(3) generates a 3x3 matrix of ones, whereas ones(3,2) generates a 3x2 matrix of ones.

#### Brackets with Matrices

All the examples above have scalar entries. It is also possible to have matrix entries:

```A = [1 2; 3 4];
[A [5; 6]]

ans =

1     2     5
3     4     6

```

In this case, all matrices in the same row must have the same column dimension, and all matrices in the same column must have the same row dimension. Unfortunately, this is one place where scalars are not expanded. For example, to append a column of ones, you must do the following:

```>> [A 1]

??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
same number of rows.

>> [A ones(size(A,1), 1)]      % [A ones(2,1)] works as well

ans =

1     2     1
3     4     1

```

Notes: ones(2, 1) creates a 2×1 matrix of 1s. size( A, 1 ) returns the number of rows of A and size( A, 2 ) returns the number of columns of A.

### Colon Operator

The colon operator x:y generates a row vector (1xn matrix) of the form [x, x+1, x+2, ..., x+n] where x+n <= y and x+n+1 > y.

```>> 3:7

ans =

3     4     5     6     7

>> 1.2:7.3

ans =

1.2000    2.2000    3.2000    4.2000    5.2000    6.2000    7.2000

```

The three variable form x:dx:z generates row vector [x, x+dx, x+2*dx, ..., x+n*dx] where x+n*dx <= y and x+(n+1)*dx > y.

```>> 3:2:11

ans =

3     5     7     9    11

>> 1.2:1.25:7.3

ans =

1.2000    2.4500    3.7000    4.9500    6.2000

```

The colon operator can be used in conjunction with brackets:

```>> [1:3 5 7:10]

ans =

1     2     3     5     7     8     9    10

```

### General Constructors

General matrix and vector constructors are:

#### diag

The diag(v) function returns a matrix with specified diagonal entries.

```>> diag([1 2 3 4])

ans =

1     0     0     0
0     2     0     0
0     0     3     0
0     0     0     4

```

The diag(v, n) shifts the diagonal either to the right (positive n) or down (negative n.) diag(v, 0) is equivalent to diag(v).

.
```>> diag([1 2 3], 2)

ans =

0     0     1     0     0
0     0     0     2     0
0     0     0     0     3
0     0     0     0     0
0     0     0     0     0

>> diag([1 2 3], -2)
ans =

0     0     0     0     0
0     0     0     0     0
1     0     0     0     0
0     2     0     0     0
0     0     3     0     0

```

An alternate use of the diag function is to return the diagonal of a matrix.

#### eye

Short for identity, eye returns an identity matrix.

```>> eye(3)

ans =

1     0     0
0     1     0
0     0     1

>> eye(3,4)

ans =

1     0     0     0
0     1     0     0
0     0     1     0

```

#### linspace

The linspace(x, y, N) function generates a row vector with N linearly spaced points between x and y.

```>> linspace(3, 7, 10)     % 10 linearly spaced points between 3 and 7

ans =

3.0000    3.4444    3.8889    4.3333    4.7778    5.2222    5.6667    6.1111    6.5556    7.0000

```

The function linspace(x, y, N) is equivalent to x:(y-x)/(N-1):y.

#### logspace

The logspace(x, y, N) function generates a row vector with N logarithmically spaced points between 10x and 10y.

```>> logspace(.2, 1.2, 10)     % 10 logarithmically spaced points between 100.2 and 101.2

ans =

1.5849    2.0470    2.6438    3.4145    4.4101    5.6958    7.3564    9.5012   12.2713   15.8489

```

#### ones

The ones(n) function generates an nxn matrix of ones.

```>> ones(3)

ans =

1     1     1
1     1     1
1     1     1

>> ones(3, 4)

ans =

1     1     1     1
1     1     1     1
1     1     1     1

```

#### rand

The rand(n) function generates an nxn matrix of random values uniformly selected from the open interval (0, 1).

```>> rand(3)

ans =

0.0362    0.4919    0.2617
0.2929    0.6533    0.5844
0.8158    0.0433    0.1370

>> rand(3, 4)

ans =

0.4541    0.1952    0.0997    0.6332
0.4029    0.7694    0.0950    0.3427
0.2528    0.9234    0.9740    0.9991

```

#### zeros

The zeros(n) function generates an nxn matrix of zeros.

```>> zeros(3)

ans =

0     0     0
0     0     0
0     0     0

>> zeros(3, 4)

ans =

0     0     0     0
0     0     0     0
0     0     0     0

```