Thursday 24 April 2014

MATLAB TUTORIAL : INDEXING & MASKING

ACCESSING INDIVIDUAL ELEMENTS
>> M = [1 2 3; 4 5 6; 7 8 9]
M =  1     2     3
     4     5     6
     7     8     9
We can access individual element by specifying (row, column):
>> M(2,3)
     6
Also, note that Matlab is using 1-based index not 0-based index.

SLICING ARRAYS
>> 1:4
ans =
     1     2     3     4
We can take a portion of matrix using slice.
>> M = [1:20]
>> M2 = reshape(M,4,5)
     1     5     9    13    17
     2     6    10    14    18
     3     7    11    15    19
     4     8    12    16    20
>> M2(1:4, 3:4)
     9    13
    10    14
    11    15
    12    16
We took all rows and 3rd and 4th columns.
When we takes all rows or columns, we don't have to use specific indices, and we can use this form:
>> M2(:,3:4)
     9    13
    10    14
    11    15
    12    16

SHIFTING DATA USING SLICE
The following example assigns value of the last three element to the first three elements:
>> v = 10:10:100
    10    20    30    40    50    60    70    80    90   100

>> v(1:3) = v(8:10)
    80    90   100    40    50    60    70    80    90   100

ARRAY AS SUBSCRIPTS
>> a = 10:10:100
    10    20    30    40    50    60    70    80    90   100

>> index = [1 10 5 7 9]
     1    10     5     7     9

>> b = a(index)
    10   100    50    70    90
In the code, we made a new array by using array as subscripts to the source array.

COMPARISON
We can compare each element with a value, and the output is a type of boolean not double:
>> a = 1:10
    1     2     3     4     5     6     7     8     9    10

>> b = a < 7
     1     1     1     1     1     1     0     0     0     0
We can use this to make another array:
>> new_a = a(a<7)
     1     2     3     4     5     6
The operation only takes elements that a<7 is true. Since we have a new array b, we can get the same result by doing this:
>> new_a = a(b)
     1     2     3     4     5     6
However, we cannot do this:
>> b2 = [1 1 1 1 1 1 0 0 0 0]
     1     1     1     1     1     1     0     0     0     0

>> new_a2 = a(b2)
Subscript indices must either be real positive integers or logicals.
That's because the elements of b2 is not a boolean but a double type.

ASSIGNING USING A MASK
We can assign a value to only an element that the mask has true value:
>> a = [0 1 2 3];
>> mask = [true false true false];
>> a(mask) = [100 102]
a =
   100     1   102     3

USING MASK WHILE KEEPING THE ARRAY SIZE
Let's see how the masking changes the size of an array:
>> a = 0:10:100
     0    10    20    30    40    50    60    70    80    90   100

>> mask = a < 80
     1     1     1     1     1     1     1     1     0     0     0

>> a(mask)
     0    10    20    30    40    50    60    70
But we want to keep the size of an array unchanged while we can still applying the mask. We can achieve this by using dot(.) meaning every element:
>> a.*mask
     0    10    20    30    40    50    60    70     0     0     0
What it is doing is a element-wise multiplication with the mask!

No comments:

Post a Comment