# Creating Scalars, Vectors, Matrices and Pages

## Recap

If you are unsure of the terminology, scalar, vectors, column, row, matrix and page take a look at MATLAB: Visualising Scalars, Vectors, Matrices and Pages. We will look at inputting these in MATLAB.

## Creating a 1D Scalar in MATLAB

We can assign a scalar with a Variable Name

Scalar
to a value
1
using:

HTML

This will give the result:

Scalar=1

In this case Scalar is a 1 by 1 Array (a 1 by 1 Array has the special name Scalar):

We can open it up in the Variable Editor to see it in as a Spreadsheet:

To input more complicated items such as a vector or matrix, all terms contained within the array must be enclosed in square brackets.

HTML

This gives the same result for the Scalar:

Scalar=1

## Creating a Row Vector in MATLAB

A Row Vector is an array that has one row and multiple columns. When inputting a row vector in MATLAB we need to use a delimiter to separate out the numeric values in each column of the row. These are enclosed in

[ ]
and in MATLAB to move onto the next Column → we use the comma
,
as a delimiter.

HTML

This gives:

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right]$

Note in the Variable Editor this is listed as a 1×3 meaning 1 Row by 3 Columns.

## Creating a Column Vector in MATLAB

A Column Vector is an array that has multiple rows and one column. When inputting a column vector in MATLAB we need to use a delimiter to separate out the numeric values in each row of the column. These are enclosed in

[ ]
and in MATLAB to move onto the next Column ↓ we use the comma
;
as a delimiter.

HTML

This gives:

$\displaystyle \text{Col}=\left[ {\begin{array}{*{20}{c}} 2 \\ 4 \\ 6 \\ 8 \end{array}} \right]$

Note in the Variable Editor this is listed as a 4×1 meaning 4 Rows by 1 Column.

## Creating a Matrix

A Matrix is an array that has multiple rows and multiple columns. For this we will need to use both the

,
Delimiter (instructions to move →) and
;
Delimiter (instructions to move ↓) respectively. The convention is to use the
,
Delimiters first to construct the 1st Row Vector and then the first
;
Delimiter to move onto the 2nd Row where we can once again use the
,
Delimiters first to construct the 2nd Row Vector and then the second
;
Delimiter to move onto the 3rd Row where we can once again use the
,
Delimiters first to construct the 3rd Row Vector and so on and so forth.

If we are using the Command Window, we can type the following on one line:

HTML

If we are using a script (discussed in another guide) we can split it over multiple lines using:

HTML

This gives:

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

Note in the Variable Editor this is listed as a 4×4 meaning 4 Rows by 4 Columns.

## Delimiters , vs ;

Note when input Matrices it may help if you conceptualise the matrix as a series of rows opposed to a series of columns.

If one attempts to input them this way

HTML

The following error is flagged up:

Dimensions of arrays being concatenated are not consistent.

This is because the

,
moves only → to the next column

$\displaystyle \left[ {\begin{array}{*{20}{c}} ? & 2 & 3 & ? \\ ? & ? & ? & ? \\ ? & ? & ? & ? \\ ? & ? & ? & ? \end{array}} \right]$

whereas the

;
moves to to the first column and ↓ and to the next row

$\displaystyle \left[ {\begin{array}{*{20}{c}} ? & ? & ? & 4 \\ 5 & ? & ? & ? \\ ? & ? & ? & ? \\ ? & ? & ? & ? \end{array}} \right]$

Matlab therefore reads the following code as:

HTML

$\displaystyle \text{Mat2=}\left[ {\begin{array}{*{20}{c}} 1 & ? & ? & ? \\ 5 & ? & ? & ? \\ 9 & ? & ? & ? \\ {13} & 2 & ? & ? \\ ? & 6 & ? & ? \\ ? & {10} & ? & ? \\ ? & {14} & 3 & ? \\ ? & ? & 7 & ? \\ ? & ? & {11} & ? \\ ? & ? & {15} & 4 \\ ? & ? & ? & 8 \\ ? & ? & ? & {12} \\ ? & ? & ? & {16} \end{array}} \right]$

This explains why MATLAB flags up the error:

Dimensions of arrays being concatenated are not consistent.

Because all the cells with the ? are unspecified.

The above code can be amended using additional

[ ]
brackets. Here each column will be created and then concatenated side by side together to form the Matrix:

HTML

We can also created the four columns as separate variables and then concatenate them together:

HTML

We could also create the four rows and concatenate them together:

HTML

The following would also work:

HTML

## Logical Indexing of a Single Matrix Element

Supposing with have the Matrix Mat and we want to select individual elements from the Mat highlighted in sky blue, red, yellow, magenta, purple and green respectively, we can use logically indexing to do this:

HTML

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & {\mathbf{4}} \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

We can look up an element in a Matrix by typing the Variable Name of the Matrix followed by circular brackets with two input arguments, the first input argument is the Row and the second one is the Column. This is similar to functions which we will look at in more depth later:

HTML

Or because we are only outputting one Variable Name we can use:

HTML

Let's read the Sky Blue Element from the Matrix Mat, we see that it is on the 1st row and the 4th column.

HTML

We get the value 4 as expected

SkyBlueElement=4

Let's read the Red Element from the Matrix Mat, we see that it is on the 2nd row and the 2nd column.

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & {4} \\ 5 & {\mathbf{6}} & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

HTML

RedElement=6

Let's read the Yellow Element from the Matrix Mat, we see that it is on the 3rd row and the 3rd column.

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & {4} \\ 5 & {6} & 7 & 8 \\ 9 & {10} & {\mathbf{11}} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

HTML

YellowElement=11

Let's read the Magenta Element from the Matrix Mat, we see that it is on the 4th row and the 4th column.

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & {4} \\ 5 & {6} & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {\mathbf{16}} \end{array}} \right]$

HTML

MagentaElement=16

Let's read the Purple Element from the Matrix Mat, we see that it is on the 4th row and the 2nd column.

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & {4} \\ 5 & {6} & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {\mathbf{14}} & {15} & {16} \end{array}} \right]$

HTML

PurpleElement=14

Let's read the Green Element from the Matrix Mat, we see that it is on the 1st row and the 1st column.

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} {\mathbf{1}} & 2 & 3 & {4} \\ 5 & {6} & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

HTML

GreenElement=1

## Colon Operator

So far we have shown a very small Row vector:

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

It is pretty easy to type out in full but there is a shortcut we can use in form of the colon operator

:

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

In this case, we don't need to enclose this in Square Brackets:

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

However if we were to use the Colon operator alongside a comma, for instance

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 & {10} \end{array}} \right]$

Failure to include them:

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

$\displaystyle \text{ans}=10$

i.e. gives two output variables but only the first is assigned so the second is output as

ans
. To output this as two Variables we could use:

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

$\displaystyle \text{Scalar}=10$

Although it useful to sometimes include the Square Brackets to see more clearly what is going on.

HTML

For learning purposes we are just going to go up to 10, so you can see what's going on. When you are grounded in the fundamentals you can then go an tackle matrices that are larger.

HTML

$\displaystyle \text{x}1=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & {10} \end{array}} \right]$

We start at 10 and move in steps of 1 until we get to 10.

HTML

$\displaystyle \text{x2}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \end{array}} \right]$

With this the maximum number was set to 9.81 and the starting number was 1. The closest number that is 1 plus multiple steps of 1 to 9.81 that is less than 9.81 is 9 so we are given 9 as the maximum value.

By default the step size is 1, it however can be changed by specifying a step size in the middle:

HTML

$\displaystyle \text{x}3=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 5 & 7 & 9 \end{array}} \right]$

We do not need to specify an integer as the step:

HTML

$\displaystyle \text{x4}=\left[ {\begin{array}{*{20}{c}} {1.0} & {1.1} & {1.2} & {1.3} & {1.4} & {1.5} & {1.6} & {1.7} & {1.8} \end{array}} \right]$

## Transposing

A row vector can be transposed into a column vector and a row vector can be transposed into a column vector. To transpose in MATLAB we use

'

HTML

$\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

$\displaystyle \text{Col}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right]$

$\displaystyle \text{Row2}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]$

Matrices themselves can also be transposed. lets create a Matrix

M
and transpose it:

HTML

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & {10} \\ {11} & {12} & {13} & {14} & {15} \end{array}} \right]$

$\displaystyle \text{Mt}=\left[ {\begin{array}{*{20}{c}} 1 & 6 & {11} \\ 2 & 7 & {12} \\ 3 & 8 & {13} \\ 4 & 9 & {14} \\ 5 & {10} & {15} \end{array}} \right]$

In the transpose each element of the matrix

M(a,b)
becomes
Mt(b,a)
. For instance the element of the matrix at
M(3,2)
which is 12 re-positions at
Mt(2,3)
in the transpose matrix.

## Array Transpose

A Matrix of numbers is a 2D array. If instead of regular numbers complex numbers are used, these numbers have a real and imaginary part. The real part lies on the xy plane and the complex part lies on the z axis. Conceptually transposing should be thought of as flipping the axes around the matrix in 3D space:

Thus when transposing a matrix which contains complex numbers, the complex component will switch sign. To prevent this array transposing

.'
can be used instead (this will be discussed in more detail later on).

## The Colon Operator and Transposing

To create a Column vector quickly we can create a Row Vector and Transpose it. We can combine this in a single line.

HTML

$\displaystyle \text{y}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \end{array}} \right]$

Note we need to use the square brackets otherwise we will transpose the scalar 8 which transposes to itself and end up with the Row Vector.

## Suppressing Output

This is particularly useful if we want to look at all the rows of some hardware for example the XPS 13 9365 which has a row of 3600 pixels and column of 1800 pixels. We would be all day trying to type the 3600 and 1800 numbers respectively…

We can get:

HTML

MATLAB can calculate large arrays such as the above relatively quickly. However outputting all these to the Command Window will substantially delay the code as there is a delay so the user has a chance to read it. It is not necessary for the user to read out 3200 and 1800 sequential numbers for instance so we can suppress the output.

If we don't want to fill our Command Window with all these values we can end the statement in a

;
to suppress the output. In this case we are not using the
;
as a Delimiter as it is not present within the
[ ]
but instead at the end of the statement.

HTML

## Logical Indexing

Supposing we want to use logical indexing to make sub-selections of the Matrix Mat

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

The Red Selection is the top left hand corner of the matrix Mat. A selection of the first two rows

[1;2]
(input 1 is a column vector and selects the desired rows) and first two columns
[1,2]
(input 2 is a row vector and selects the desired columns):

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} {\mathbf{1}} & {\mathbf{2}} & 3 & {4} \\ {\mathbf{5}} & {\mathbf{6}} & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

HTML

$\displaystyle \text{RedSelection}=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 5 & 6 \end{array}} \right]$

As MATLAB knows the first input for logical indexing has to be a column vector and the second input has to be a row vector, it will automatically transpose these. Thus the following three lines of code are all equivalent to the above.

HTML

The Blue Selection is the Last Column.

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} {1} & {2} & {3} & {\mathbf{4}} \\ {5} & {6} & 7 & {\mathbf{8}} \\ 9 & {10} & {11} & {\mathbf{12}} \\ {13} & {14} & {15} & {\mathbf{16}} \end{array}} \right]$

We can select it using the following:

HTML

$\displaystyle \text{BlueSelection}=\left[ {\begin{array}{*{20}{c}} 4 \\ 8 \\ {12} \\ {16} \end{array}} \right]$

However we know that 4 is the end value of the matrix, so we can modify the code above to:

HTML

We also know that it doesn't matter if we specify the input 1 as a Column Vector so we can drop the

'

HTML

Since we are select all Rows another shortcut is to use a

:
which on its own represents all elements.

HTML

Because we are specifying all values and just the end value for inputs 1 and input 2 respectively, we don't need to enclose these in the

[ ]
thus the above can be simplified to:

HTML

The Yellow Selection is of the first 2 rows of the 2nd Last Column:

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} {1} & {2} & {\mathbf{3}} & {4} \\ {5} & {6} & {\mathbf{7}} & {8} \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]$

HTML

$\displaystyle \text{YellowSelection}=\left[ {\begin{array}{*{20}{c}} 3 \\ 7 \end{array}} \right]$

Since it is the Second Last Column we can also refer to it as the

end-1
column.

HTML

We don't need the Second set of square brackets and we can use a

,
;

HTML

And finally the Green Selection which is the 3rd and 4th Rows of the 1st three Columns:

$\displaystyle \text{Mat}=\left[ {\begin{array}{*{20}{c}} {1} & {2} & {3} & {4} \\ {5} & {6} & {7} & {8} \\ {\mathbf{9}} & {\mathbf{10}} & {\mathbf{11}} & {12} \\ {\mathbf{13}} & {\mathbf{14}} & {\mathbf{15}} & {16} \end{array}} \right]$

HTML

$\displaystyle \text{GreenSelection}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \end{array}} \right]$

This can also be defined from the end points:

HTML

Suppose we want to combine the fragments to create the following:

Conceptually we need to bring the

YellowSelection
and
RedSelection
together:

HTML

Then we can bring the

GreenSelection
and
YellowRedSelection
together:

HTML

Finally we can bring the

BlueSelection
and
GreenYellowRedSelection
together:

HTML

This can also be done in one line:

HTML

## Creating a 3D Array

We can visualise a 3D Array in Excel. We can see that it consists of three Pages and each Page is a 2 by 2 Matrix.

We can get started by Creating the Matrices corresponding to the individual pages:

HTML

$\displaystyle \text{Page1=}\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right]$

HTML

$\displaystyle \text{Page2=}\left[ {\begin{array}{*{20}{c}} 5 & 6 \\ 7 & 8 \end{array}} \right]$

HTML

$\displaystyle \text{Page3=}\left[ {\begin{array}{*{20}{c}} 9 & {10} \\ {11} & {12} \end{array}} \right]$

Once the pages are made, logical indexing can be made this time when assigning the variable name. The first and second inputs to the variable name will be

:
and
:
as all rows and all columns are selected. The third input will be the page number which will be assigned to 1, 2 and 3 respectively.

HTML

The first line of code will just duplicate Page 1:

Adding the second page will create a 2 Rows by 2 Columns by 2 Pages Book:

Adding the third page will create the a 2 Rows by 2 Columns by 3 Pages Book as desired:

This site uses Akismet to reduce spam. Learn how your comment data is processed.