* Vectors* and

*are used to store sets of values. The values should of same type (e.g. numerical values). A vector can be either a row vector or a column vector. If a vector has n elements, a row vector would have the dimensions $1\ x\ n$, and a column vector would have the dimensions $n\ x\ 1$. A scalar (one value) has the dimensions 1 × 1.*

**matrices**A matrix can be visualized as a table of values. The dimensions of a matrix are r × c, where r is the number of rows and c is the number of columns. This is pronounced “r by c.” Therefore, vectors and scalars are actually just subsets of matrices.

From the figure above, the scalar is $1\times 1$, the column vector is $3\times 1$ (3 rows by 1 column), the row vector is $1\times 4$ (1 row by 4 columns), and the matrix is $3\times 3$. All the values stored in these matrices are stored in what are called elements.

A vector in MATLAB is equivalent to what is called a one-dimensional array in other languages. A matrix is equivalent to a two-dimensional array

Creating Row Vectors

Creating Column Vectors

Creating Matrices

Dimensions in Vectors and Matrices.

Changing Dimensions in Matrices

Referring to and Modifying Elements

Vector and matrix arithmetic

## Creating Row Vectors

A row vector can be created in many ways. First the most direct way is putting the desired values in square bracket. The value (elements) ought to be separated by either spaces or commas. Note: the elements could also be the variable values. For example let us create a row vector of values 1 to 10.

<p>

</p><p></p>

The row vector can also be created using the **colon operator. **For the regularly spaced vectors, the colon operator can be used to** iterate **through the values. In this case the square brackets are not necessary.

With colon operator one can specifier the **step value**. The syntax used is `first:step:last`

*. *For example, let us create a vector of all integer value from 1 to 20 at a step of 2.

The step use could also be negative e.g. generate the vector `[9 6 3 0]`

.

The third way of creating a vector is using function. Syntax `linspace(x,y,n)`

, This creates a vector with `n`

values in the inclusive range from `x`

to `y`

. If we apply `linspace`

to all of the examples above we have:

## Creating Column Vectors

One way to create a column vector is by explicitly putting the values in square brackets, separated by semicolons. For example

Creation of column vectors by the **colon operator** is not direct as for the row vector. To create a column vector by using the colon operator, the row vector is created first, then the row vector is** transposed** to get a column vector. MATLAB has a built-in operator, the **apostrophe**,
for **transposing** vectors and matrices. (If the use of linspace to create a column vector, the same procedure applies). For example

## Creating Matrices

Matrix variable is really just a generalization of creating row and column vector variables. That is, the values within a row are separated by either spaces or commas, and the different rows are separated by semicolons. For example, the matrix variable mat is created by explicitly typing values:

A matrix variable is a generalization of creating row and column vector variables. That is, the values within a row are separated by either spaces or commas, and the different rows are separated by semicolons. There must always be the same number of values in each row of the matrix. For example, the matrix variable mat is created by explicitly typing values in the command window:

Iterators can also be used for the values on the rows using the colon operator or linspace; for example:

Matrices can also be created from random values using `rand`

and `randi`

function. For the rand function, if a single value * n* is passed to it, an

*×*

**n***matrix will be created, or passing two arguments will specify the number of rows and columns. For the randi, syntax*

**n**`randi(x,[M,N])`

creates an *by*

**M***matrix of random integers, each in the range from*

**N***0*to

*x*. For example:

MATLAB also has several functions that create special matrices. For example, the `zeros()`

, `ones()`

and `eye()`

- functions creates a matrix of all zeros, ones and identity matrix respectively. Like `rand()`

either one
argument can be passed (which will be both the number of rows and columns), or two arguments (first the number of rows and then the number of
columns). These special matrices are important in initializing matrices as we shall observe in the next chapter.

## Dimensions in Vectors and Matrices

The `length()`

and `size()`

functions in MATLAB are used to find array dimensions.
The `length()`

function returns the number of elements in a vector. The `size()`

function returns the number of rows and columns in a matrix. For a matrix, the `length()`

function will return either the number of rows or the number of columns, whichever is largest. For example:

The `size()`

function returns two values, so in order to capture these values in separate variables we put a vector of two variables on the left of the assignment.
The variable `r`

stores the first value returned, which is the number of rows, and `c`

* *stores the number of column as shown in the graph below.

MATLAB also has a function, `numel()`

, which returns the total number of elements in any array (vector or matrix):

## Changing Dimensions in Matrices

MATLAB has several built-in functions
that change the dimensions or configuration of matrices, including `reshape()`

, `fliplr()`

, `flipud()`

and `rot90()`

The `reshape()`

function changes the dimensions of a matrix. The syntax is `reshape(matrix,row,cloumn)`

The `fliplr()`

function “flips” the matrix from left to right (in other words the
left-most column, the first column, becomes the last column and so forth),
and the `flipud()`

* *functions flips up to down. The

`rot90()`

*function rotates the matrix counterclockwise 90 degrees.*

## Referring to and Modifying Elements

Elements in a vector are numbered sequentially. Each element number is called the index, or subscript. An element in a vector is accessed by using the name of the vector variable and the index or subscript in parentheses. Consider the table below.

Element with index/subscipt 4 would be accessed as follows:

For matrices, Using just one colon by itself for the row subscript means all rows, regardless of how many and colon for the column subscript means all columns as shown.

When a single index is used with a matrix then MATLAB unwinds the matrix column by column. A single element can be accessed by using the index that points to that element (row and column position).

**Note:** Most of the function operate to the matrices and vectors column-wise.

## Vector and matrix arithmetic

We can now explore making matrices and doing arithmetic with them. There are some new arithmetic operations that you did not see before in linear algebra, but they will be useful nonetheless.

The results in the command window for the above code is:

## Exercise

Create a vector

`x`

containing integer numbers from $1$ to $100$. Create a vector`y`

containing numbers $1$, $0.9$, $0.8$, $0.7$, . . . $0.1$, $0$ in this order.Create a $3\times 3$ matrix containing only ones. Create an $8\times 1$ matrix containing only zeros. Create a matrix $5\times 2$ with all elements equal to $0.37$

Given matrix

`m = [1, 2, 3; 2, 1, 5; 4, 6, 4; 2, 3, 2]`

, create its submatrix`n`

containing first two rows and the first and the third column (i.e., row indexes`i = 1, 2`

and column indexes`j = 1, 3`

).Create a vector

`x = [3, 1, 2, 5, 4]`

. From`x`

create`y`

containing the same elements in the reverse order. Find indices of elements greater than $2$, create`z`

containing elements of`x`

which are smaller than $4$.

## Comments

You need a Ludu account in order to ask the instructor a question or post a comment.

Be the first to comment!