Let’s just use an excel spreadsheet with no numbers on it to give an overview of the different types of arrays. Once we understand these Arrays we can go ahead and look at inputting them into Octave/MATLAB and looking at some of their properties.

Scalar – 1 by 1 Array

In other words a scalar is just a single number.

Column Vector – m by 1 Array

A Column Vector can be considered as a list of scalars, listed in a column. These can be a list of times, or a list of distances, a list of speeds or a list of prices.

Row Vector – 1 by n array

A Row Vector can be considered as a list of scalars, listed in a row. This information can just be the same as presented in the Column Vector but in a different orientation. The best orientation depends on how the data can be manipulated later on, which will be discussed later on however Row Vectors can always be transposed into Column Vectors and vice versa.


Transposing switches Rows and Columns and vice versa. This allows one to convert a Column Vector into a Row Vector and vice versa.

Matrix – m by n Array

A matrix can be thought of as a rectangular grid of scalars.

A matrix can be considered as the building up of a row by use of equally sized column vectors:

Taking the example of using a column vector as a list of prices of goods in a shop. The first column could be Asda, the second Tescos, the thirds Morrisons, the fourth Lidl and the fifth Aldi’s… for instance.

Alternatively it can be considered as the building up of a column by use of equally sized row vectors:

An example of a Matrix is also the screen of your computer or phone or the sense of the imaging CCD in your camera. It is a rectangular object with many pixels and you can think of the pixel as a square in the excel spreadsheet. My Dell XPS 13 9365 has 3200 by 1800 of these squares. For a black and white image to display each pixel has to give off a specified brightness. For 8 Bit there are 256 (0-255) levels of grey.

3D Array m by n by p Array

This may look more complicated but think of this like flicking through pages in a book, each page has the same dimensions in terms of m and n but p (the page) has changed. In this case, the sheets of the spreadsheet.

A colour image is an example of a 3D Array. Instead of there being a single grey channel, there are now three colour channels corresponding to the three primary colours red, green and blue. For 8 bit there are 256 levels (0-255). Any other colour can be made by mixing different values of these colours.

Labelling Cells of a Matrix

Here the black line represents the boundary of the Matrix. By convention, we say the matrix has m rows by n columns often abbreviated a m by n matrix. In this case it is a matrix with 14 rows by 10 columns, known as a 14 by 10 matrix. Do not get the labelling mixed up when it comes to rows and columns. If it helps, think of entering a block of flats, which has a 3 floors and each floor has 4 apartment. You are going to find the floor before you look for the room number. The matrix element that we have highlighted is positioned at row 4 and column 2 (we’ve looking for apartment number 4.2 and have first climbed to the 4th flight then looked for apartment 2).

This is an example of a 4 by 8 matrix and the highlighted cell is at position 3,7.

Creating a Matrix in a Script File or via the Command Window

Previously we assigned the value of 1, to a variable name of x using the command:


\displaystyle x=1

In this case x is a 1 by 1 array otherwise known as a scalar.

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


\displaystyle x=[1]

Previously we seen when using the nthroot function that it had the form:


The two input arguments were separated by a comma , . For a row vector we also use the , to jump ’ over to the next cell.

Thus to assign the above to the variable row we type in:


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

If instead of jumping right, we want to jump ” we can use the semicolon ;

Thus to assign the above to the variable col we type in:


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

To make a Matrix that is 4 by 4 like above we need to use both commas , and semicolons ; Note the comma . moves onto the next column while the semi-colon ; moves onto the start of the next row.


\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]

For convenience, in a script file this can also be written over multiple lines.


Size of the Matrix

Although we can readily see the size of these matrices and read them off the variable editor, it is sometimes useful to read off the size of an array in Octave/MATLAB so this information can be used in more complex code. We can use the function size to get the dimensions of an array. The input argument x should be the array under investigation. The output arguments should be in square brackets [ ] and separated by a comma , i.e. of the same nature as a row vector.


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

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

\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]


\displaystyle \begin{array}{l}m=1\\n=3\end{array}


\displaystyle \begin{array}{l}m=4\\n=1\end{array}


\displaystyle \begin{array}{l}m=4\\n=4\end{array}

Note the sizes of arrays are also shown in the workspace.

Logical Indexing

Logical indexing is extremely useful, if one wants to select only part of a Matrix, e.g. if one has a Matrix loaded up and wants to extract the first column of it, in order to plot it (I will make another guide on plotting). Here we will look at selecting individual Matrix elements and then vectors and columns and submatrices from a larger matrix. We also use concatenation to join smaller matrices or vectors together to make a larger matrix. Previously we seen when using the nthroot function that it had the form:


The two input arguments were separated by a comma , and enclosed by round brackets ( ). The output argument could be specified, otherwise it would just be printed out in the Command Window. Logical indexing of matrices takes on a similar form, Element=Mat(x,y) with the matrix acting in a similar manner to the function and the input arguments x and y selecting the row and column respectively, the output argument Element gives the value of the element of the Matrix. For instance we can look up the 2nd row and 2nd column of the Matrix Mat and assign it a variable name Mat22:

\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]


\displaystyle \text{Mat22}=6

Suppose we want to look up the matrix elements that are highlighted red, yellow, magenta, purple, sky blue and green.


\displaystyle \text{Mat22}=6


\displaystyle \text{Mat33}=11


\displaystyle \text{Mat44}=16


\displaystyle \text{Mat42}=14


\displaystyle \text{Mat14}=4


\displaystyle \text{Mat11}=1

So far for logical indexing using Element=Mat(x,y) we have used scalar input arguments x and y to select the row and column respectively. We can instead use vectors as input arguments and correspondingly create output arguments which contain multiple elements i.e. the output arguments are subselections that are themselves vectors or submatrices.

Use logical indexing to select the following parts of Mat, highlighted in red, yellow, green and blue.

For Mat_r we are looking for column 1 and 2 and row 1 and 2. Thus we have the first input argument as [1;2] and the second input argument as [1,2]


\displaystyle \text{Mat }\!\!\_\!\!\text{ r}=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 5 & 6 \end{array}} \right]

For Mat_y we are looking for column 1 and 2 and row 3. Thus we have the first input argument as [1;2] and the second input argument as [3]


\displaystyle \text{Mat }\!\!\_\!\!\text{ y}=\left[ {\begin{array}{*{20}{c}} 3 \\ 7 \end{array}} \right]

For Mat_g we are looking for column 3 and 4 and row 1,2 and 3. Thus we have the first input argument as [3;4] and the second input argument as [1,2,3]


\displaystyle \text{Mat }\!\!\_\!\!\text{ g}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \end{array}} \right]

For Mat_b we are looking for column 1,2,3 and 4 and row 4. Thus we have the first input argument as [1;2;3;4] and the second input argument as [4]


\displaystyle \text{Mat }\!\!\_\!\!\text{ b}=\left[ {\begin{array}{*{20}{c}} 4 \\ 8 \\ {12} \\ {16} \end{array}} \right]

For logical indexing

  • we treat the Matrix( ) as a function
  • input arguments are separated by a comma ,
  • the first input argument selects the row(s)
  • the second input argument selects the column(s)
  • the output argument is the matrix element(s)
  • the input arguments may be scalars in which case square brackets are optional
  • the input arguments may be vectors which require the [ ] brackets
  • if using vector input arguments the first input argument is a column vector and the second input argument is a row vector*

With the code above, I have strictly input a column vector as the first input argument and a row vector as the second input argument as it consistent with, the code used to create the Matrices above and it’s best to be consistent for learning concepts. When using vector input arguments in logical indexing or in functions, Octave/MATLAB will often automatically convert a row vector into a column vector or a column vector into a row vector where needed. Octave/MATLAB document often favours the comma . to the semi-colon ; and initially when learning, this confused me. The following three lines of code are treated as the same:





Matrices can be built up using pre-existing variables opposed to typing every single number into the Command Window every time. However in order to concatenate rows (join rows together from two different variables) they must have the same number of rows m. Likewise in order to concatenate columns together (join columns together), they must have the same number of columns, n. Lets look at the size of the fragments we obtained from Mat

\displaystyle \text{Mat }\!\!\_\!\!\text{ r}=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 5 & 6 \end{array}} \right]


\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=2\end{array}

\displaystyle \text{Mat }\!\!\_\!\!\text{ y}=\left[ {\begin{array}{*{20}{c}} 3 \\ 7 \end{array}} \right]


\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=1\end{array}

\displaystyle \text{Mat }\!\!\_\!\!\text{ g}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \end{array}} \right]


\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=3\end{array}

\displaystyle \text{Mat }\!\!\_\!\!\text{ b}=\left[ {\begin{array}{*{20}{c}} 4 \\ 8 \\ {12} \\ {16} \end{array}} \right]


\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=1\end{array}

Suppose we want to construct Mat2 from Mat_r, Mat_y, Mat_g and Mat_b

Visually we can see that it is possible to concatenate Mat_y and Mat_r together:

We can check the sizes using MATLAB/Octave:


m=2, n=1


m=2, n=2

Because m, the number of rows match it is possible to concatenate the rows.


\displaystyle \text{Mat }\!\!\_\!\!\text{ yr}=\left[ {\begin{array}{*{20}{c}} 3 & 1 & 2 \\ 7 & 5 & 6 \end{array}} \right]


\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=3\end{array}

Now that we have Mat_yr, we can see that visually we can concatenate Mat_g and Mat_yr

Once again we can check the sizes using MATLAB/Octave:


m=2, n=3


m=2, n=3

This time both m and n match but we are interested in column concatenation (joining the columns together) so it is n matching that is important.


\displaystyle \text{Mat }\!\!\_\!\!\text{ yrg}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \\ 3 & 1 & 2 \\ 7 & 5 & 6 \end{array}} \right]


\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=3\end{array}

Looking at:


We know from before that:


Therefore in one line


Now that we have Mat_gyr we can compare it to Mat_b and we can see that visually the number of rows match:

Once again we can check the sizes using MATLAB/Octave:


m=4, n=1


m=4, n=3

Because m, the number of rows match it is possible to concatenate the rows.


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


\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=4\end{array}

However we know:



So we can rewrite:


We also know:


So we can rewrite:


Note Matrices have to be rectangular with all rows having the same number of elements as each other and all columns having the same elements as each other. If you attempt to input a matrix which doesn’t have matching dimensions you will get a dimensions mismatch error.

For example changing the following line:




i.e. replacing a single comma with a semi-colon will produce the error:

For practice, you should try making Mat_b, Mat_r, Mat_g and Mat_y from Mat2. Then use these to recreate Mat in multiple lines and in a single line.


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


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


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


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

Matrices themselves can also be transposed. lets create a Matrix M


\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]

Next lets go ahead and transpose it:


\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.

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:

Before the transpose the x values are shown as the blue arrow and the y values as the red arrow. The third axis z axis is the green arrow and is imaginary. If you look at the screen and take your right hand and stretch it out to your right hand side and point, and assign your index finger to the blue arrow, next stretch your middle finger out and thumb so they are at right angles to each other. Rotate our arm counterclockwise so you are still pointing to the right, but your middle finger is pointing down, assign this finger to the red arrow. You should see that your thumb not points in the way towards the screen. If you now keep your fingers in that position and bend your arm so your index finger now points towards the ground, and your middle finger points to your right, you’ll see your thumb now points out the way away from the screen. In other words you have transposed your index finger and middle finger and your thumb points in the negative direction.

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

row=[1,2,3,4] is pretty easy to type in but what happens if we want to look at all the rows of some hardware for example the XPS 13 9365 which has a row of 1800 pixels. We would be all day trying to type the 1800 numbers. Instead we can use the colon operator to type in row=[1:1800]=1:1800. If we want the column vector instead, we must make a row vector first and then transpose it col=[1:3200]’ when reading such a large number of pixels we typically wouldn’t output to the Command Window as most users can comprehend a huge list of numbers directly. Thus we would end the lines in a semi-colon.

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.


\displaystyle \text{x}=\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.


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


\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:


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


\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]

Linear Spacing

As we can see the Colon operator may miss the end point we specify if we are using decimal points.


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

If we want both the start and the end point we are better to use the linspace function which has the form OutputArray=linspace(x1,xe,n) and has three input arguments, x1 the first number, xe the last number and n the number of points, which it will use to create the OutputArray


\displaystyle \begin{array}{l}\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]\\\text{x3 }\!\!\_\!\!\text{ }2=\left[ {\begin{array}{*{20}{c}} 1 & {3.25} & {5.5} & {7.75} & {10} \end{array}} \right]\end{array}


Supposing we want to create a matrix C which has 2 columns the first going from 1 to 5 and the second going from 6 to 10. We could set A=[1:5]’ and B=[6:10]’ and then concatenate A and B using C=[A,B]. However if we have a larger matrix, we could save some time using the reshape function. This has the form Moutput=reshape(Minput,[m,n]) where Minput is the input matrix and m and n are the number of rows and number of columns you wish to reshape the output matrix Moutput to. In this case we can use


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



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

We could also use the data from C to reshape


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

Reshape works in column order. It takes the first column of the input matrix and then adds the second column to the bottom of it and the third column to the bottom it and so on and so forth… and then it reshapes them to create the output matrix according to the specified dimensions m and n.

We have previously made the matrix that is row ordered. We can recreate it using reshape but because it is row ordered we will need to transpose it.


\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]


A Matrix of zeros

One useful function is zeros which allows one to make a Matrix full of zeros. Why you ever want to do that? Take for example the screen of the XPS 13 9365, to conserve battery power you dim the screen when the system is sleeping. How do you do that? You do that by sending zeros to every pixel on the screen. The function zeros has the form Zmn=zeros(m,n) where the input argument m is the number of rows and n is the number of columns and Zmn is the output zero matrix. For example:


\displaystyle \text{Z52}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{array}} \right]


A Matrix of ones




Creating a 3D Array



Leave a Reply

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