Matrix Indexing 2

  • In order to select randomly positioned elements (e.g. (2,3), (1,4) and (1,2)) within a matrix, you need to use the program’s  linear indexing system. This numbers the elements as if they were placed in a line. The numbering system starts from the top left moves downwards and continues at the top of the next column.
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

 

  • For example, in matrix M (below), elements 1 to 4 are 16, 5, 9 and 4 and elements 5 to 8 would be 2, 11, 7 and 14 and so on. Thus, in order to select elements (2,3), (1,4) and (1,2):

5

EXERCISE 3

2

  1. From matrix J make a 4-by-4 matrix P that comprises the first four rows and last four columns of J
  2. From P make a 3-by-3 matrix K that comprises the first three rows and columns of P
  3. From K, make a 2-by-2 matrix C with the same elements it would have if it were declared by the code C=[21 ,27; 7, 26].
  4. By hand, write down the matrix, L made by the code: L=J([1, 7, 8; 22, 17 18; 9, 33, 4])
  5. Using the attributes of J declare a scalar N, with a magnitude of 36.

MISCELLANEOUS EXERCISE

  • Logical operators use a binary system (1 and 0) to act as some sort of filter. Generally, a 1 signifies that the statement being analysed is true and a 0 implies it’s false. The 1 and 0 arrangement of a logical operator is usually used to harvest certain elements of a matrix.  For example, a logical matrix B is shown below:

3

For a matrix A:

4

  • What do you reckon would be the output of the code A(B)? Write down your guess by hand and run the code to see what it actually does.
  • What would be the output of the code C = isprime(A)? (Hint: it creates a logical matrix.)
  • Make a note of what the following do: A(~C) and A(~C)=0. What is the difference between the two? If you had to characterise the action of the ~ operator in words, what would you say is its function? (more on non-arithmetic operators such as this would be covered later)

[Show]

(For more on Matrix indexing click on the link: Advanced Matrix indexing and Examples)