Arrays

Overview

Teaching: 30 min
Exercises: 0 min
Questions
  • How can I access subsets of data?

Objectives
  • Select individual values and subsections from data.

We have talked about how Matlab stores all data in matrices. Let’s learn how to subset data in matrices, and to operate with matrices.

Unlike in other programming languages, Matlab does not require you to declare the array or define its size. Actually, it is possible to resize arrays in Matlab when needed.

Matrix indexing

There are three ways of accessing the elements of a matrix with Matlab.

Let’s create an example array using the “magic” function.

>> M = magic(4)
M =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

Indexing with two subscripts

We want to access a single value from the matrix. For example, number 15 which is in row 4 and column 3. The element can be selected providing the index in parenthesis.

>> M(4,3)
ans =

    15

Now we want to select a whole row, row 2. The colon notation : in Matlab selects a range.

>> M(2,1:4)
ans =

     5    11    10     8

Instead of using the number 4, for which we need to remember that the matrix has 4 columns, we can use the end operator that refers to the last item. So,

>> M(2,1:end)
ans =

     5    11    10     8

A single : in a subscript position is shorthand notation for 1:end and is often used to select entire rows or columns:

>> M(2,:)
ans =

     5    11    10     8

To select a column we would do

>> M(:,4)
ans =

    13
     8
    12
     1

If we go back to our inflammation data we can select the inflammation of the first patient over the 40 days of treatment by selecting

>> first_patient = patient_data(1,:);

And we can select the inflammation of all patients on day 19 by doing

>> inflammation_day_19 = patient_data(:,19);

Linear indexing

We can also refer to the items in a matrix by using only one index. In this case Matlab is treating the array as if the elements in the Matrix were strung out in a long column vector, by going down the columns consecutively, as in:

16
5
9
4
2
11
7
14
3
10
...

So, the ninth element in that array will be the number 3.

>> M(9)
ans =

     3

We can use a vector of indices to select elements in a matrix:

>> M([5,8,16])
ans =

     2    14     1

A useful function that uses linear indexing is the function find. If we want to find the elements of the array that are larger than, say, 10, we will do

>> idx_largerthanten = find(M>10)
idx_largerthanten =

     1
     6
     8
    12
    13
    15

idx_largerthanten is storing the linear indices of M that correspond to values larger than 10. And to see which values are these,

>> M(idx_largerthanten)
ans =

    16
    11
    14
    15
    13
    12

Logical indexing

Logical indexing is done by having an array of the same size as the array we are considering, that is a logical array. This means that the array has only zeroes and ones. For example, many Matlab functions that start with is return logical arrays.

For example, if we wanted to find the prime values in array M we would use the prime function.

>> primeM = isprime(M)
primeM =

  4×4 logical array

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

And we can operate using this arrays. We can, for example, substitute all the prime numbers with the number 2.

>> M(primeM) = 2
M =

    16     2     2     2
     2     2    10     8
     9     2     6    12
     4    14    15     1

Logical arrays can be created using logical operators. When the condition is true, a 1 will appear. Whe the condition is not true, the corresponding element of the array will be a zero. For example, to select the elements smaller than five:

>> logic_smallerthanfive = M<5
logic_smallerthanfive =

  4×4 logical array

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

NOTE: explanations and figures of this lesson have been taken from the Matrix Indexing in Matlab article

Key Points

  • M(row, column) indices are used to select data points

  • : is used to take slices of data