So far we have learned how to define a matrix in Scilab. In this article we’ll learn how to extract a particular value from a matrix or a particular set of values (e.g. one column, one row, a sub-matrix).

First let’s define a matrix which is going to be our test variable. We’ll define a non-square matrix, named `testMatrix`

, with 3 rows and 4 columns just to make this example more generic:

-->testMatrix = [1 2 3 4;11 12 13 14; 21 22 23 24] testMatrix = 1. 2. 3. 4. 11. 12. 13. 14. 21. 22. 23. 24. -->

#### Extracting one element from a matrix

To extract just one element from a matrix we have to specify from which row and what column we want our value to be extracted. For example we want to put in a separate variable, named `testElement`

, the value from line 2 and column 3 of the matrix.

`-->testElement = testMatrix(2,3)`

`testElement =`

`13.`

`-->`

In the same way we can extract whatever value we want from the matrix just by specifying the index of the row and column.

#### Extracting one row from a matrix

Suppose we want to extract a complete row from out test matrix. There are several ways of doing this and we’ll learn a couple of techniques.

First method of row extraction is using the colon operator “:”

We want to extract the second row from out test matrix and put the values into a new variable named `testRow`

. To do this, as arguments of the matrix we insert the row index, followed by the colon operator. Basically this tells Scilab to extract the values from all the columns corresponding to the second row:

`-->testRow = testMatrix(2,:)`

`testRow =`

`11. 12. 13. 14.`

`-->`

Another method of doing the same extraction is using the explicit definition of the start and end column index. Since we know that we have 4 columns, we tell Scilab to extract the values starting with the 1st column up to the 4th column, corresponding to the 2nd row:

`-->testRow = testMatrix(2,1:4)`

`testRow =`

`11. 12. 13. 14.`

`-->`

This method makes sense to use only if we want to extract just a part of the columns, not all of them. For example if we want to extract only the first 3 columns, corresponding to the 2nd row:

`-->testRow = testMatrix(2,1:3)`

`testRow =`

`11. 12. 13.`

`-->`

If we are interested in all the elements of the columns for a particular row it makes sense to use the colon operator because we don’t have to specify the index for the first and last column. This way the code is more simple, compact and robust.

#### Extracting one column from a matrix

The same technique applies if we want to extract a column. The difference being that we specify first how many rows we want to extract for a particular column. If we want to extract all rows for a particular column we use the colon operator “:”

`-->testColumn = testMatrix(:,3)`

`testColumn =`

`3.`

`13.`

`23.`

`-->`

In the example above we defined a new variable `testColumn`

which contains the values of the 3rd column of the matrix `testMatrix`

.

If we need only the first two values of the 3rd column we have to define the start row and the end row:

-->testColumn = testMatrix(2:3,3) testColumn = 13. 23. -->

#### Extracting a sub-matrix from a matrix

Let’s say that we have a 3×4 matrix from which we want to extract the values of the last 2 rows and 2 columns. In this case we have to input as arguments the start and end index for the row and the start and end index for the column:

-->testSubMatrix = testMatrix(2:3,3:4) testSubMatrix = 13. 14. 23. 24. -->

Another way of extracting data from a matrix is by using the `$`

(dollar) symbol. The $ symbol stands for the last row or the last column in a matrix. The good thing about it is that we do not have to know the index of the row or column.

On the examples below we are going to use the `testMatrix`

variable to:

- extract the bottom left element:

-->testMatrix($,$) ans = 24. -->

- extract the last column:

-->testMatrix(:,$) ans = 4. 14. 24. -->

- extract the last row:

-->testMatrix($,:) ans = 21. 22. 23. 24. -->

- extract the elements contained between the row before the last one and the last three columns:

-->testMatrix($-1,$-2:$) ans = 12. 13. 14. -->

A synthesis of the variables used in this article can be viewed in the Variable Browser within Scilab. If the window is not visible you can display it by using the entering the `browsevar()`

command at the console:

With these examples understood you can now practice extracting the values you need from a given matrix. These techniques are quite useful when dealing with data processing. You’ll find that every time you need to read data from an input file or from an existing workspace variable, defining the correct arguments for the matrix will help you develop robust and easy to read Scilab scripts.

For any questions, observations and queries regarding the article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

## Ajay Venkata

I need to extract the elements as

testmatrix = [1 2 3 4; 11 12 13 14];

It means I need to exempt the last row.