Tony davies has decided to explore the magical world of Matrix Algebra. He will demonstrate some very basic properties of a matrix and how they can be manipulated in MATLAB. In subsequent articles he will be assisted by his friend and tutor, professor Tom fearn, and we will attempt to solve a real spectroscopic problem.
Tony davies has decided to explore the magical world of Matrix Algebra. He will demonstrate some very basic properties of a matrix and how they can be manipulated in MATLAB. In subsequent articles he will be assisted by his friend and tutor, professor Tom fearn, and we will attempt to solve a real spectroscopic problem.
Tony davies has decided to explore the magical world of Matrix Algebra. He will demonstrate some very basic properties of a matrix and how they can be manipulated in MATLAB. In subsequent articles he will be assisted by his friend and tutor, professor Tom fearn, and we will attempt to solve a real spectroscopic problem.
24 Spectroscopy Europe 2000 Spectroscopy Europe 12/2 (2000)
When I began these columns, many years ago, in Spectroscopy World 1 I made the promise that Matrix Algebra would be kept to a minimum. My intention was (and still is) to make sure that the column was accessible to all spectro- scopists and not just those who were mathematically adapt. However, I have decided that it is time to move forward (we may not have reached the Mil l ennium yet but at l east the Millennium bug is behind us) and so together we are going to explore the magical world of matrix algebra! One of the facts that help me make this decision is that matrix algebra pro- grams are becoming more accessible and they are being much more widely used but I should make it clear that I am learning with you (or perhaps one column ahead of you). Until this year I may have been using matrix algebra inside commercial programs but they were transparent to me and beyond my control. I did not have a matrix algebra program. In order to write these columns I obviously needed access to one and Math Works Inc. have very kindly given me a copy of MATLAB for this purpose. Most of the chemometric books on my bookshelf have a chapter or an appendix on matrix algebra and they are all very similar. They are mainly composed of a list of matrix algebra operations with very little insight as to when or why you should want to use them. I would like to try a different approach. In this first article I will demonstrate some very basic properties of a matrix and how they can be manipulated. In subsequent articles I will be assisted by my friend and tutor, Professor Tom Fearn, and we will attempt to solve a real spectroscopic problem and explain how matrix alge- bra has been utilised. I was so impressed by the introduc- tion to MATLAB 2 that I realised that I was not going to be able to improve on it; so I decided to borrow it! Figure 1 is a fragment of an etching by Albrecht Drer (14711528) and it shows a matrix. This matrix has some rather special properties and we can demonstrate them rather conveniently as a MATLAB matrix. There are many ways of entering data into MATLAB. When there is not much data it is quite simple to type it. The command: A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] will produce: A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 This is Drers matrix and from now on we can refer to it as A perhaps we should have named it after Drer and called it D. Well we can do that by saying: D = A Why should we be interested in D? Because it is known as a magic square. What is magical? Well, whichever way you go every line of four numbers adds up to the same number. If we com- mand; sum(D) MATLAB responds with: ans = 34 34 34 34 it has just added up each column and you might like to check that it has got the correct answer. I have just used a matrix algebra term but you will not have noticed it because you know what the word col umn means. It has the same meaning in matrix algebra; similarly row has the expected meaning but you might not guess that the individual numbers (or rather the place where they are stored) are know as ele- ments. If we are being very correct the columns and rows are called col- umn vectors and row vectors. A vec- tor is defined as a row or column of numbers. It will often happen that we need to change our mind about which are columns and which are rows but in matrix algebra there is a very simple way of reorganising the data. It is known as transposition and is referred to as D T . The command; D produces: ans = 16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1 so the rows are now columns and the columns are rows. MATLAB is orient- ed to operating on columns so that usually if we want to do an operation on the rows we first transpose and do it on the columns. Therefore the com- mand; sum (D) produces ans = 34 34 34 34 which is the sums of the rows. All matrices contain diagonals. The diagonal from top left to bottom right is mathematically important and is called the main diagonal while the one from top right to bottom left is known as the antidiagonal. We get the elements of the main diagonal very easily because there is a command for it. Entering diag(D) produces ans = 16 10 7 1 The magical world of matrix algebra Tony Davies Norwich Near Infrared Consultancy, 75 Intwood Road, Cringleford, Norwich, NR4 6AA, UK. Figure 1. Detail from Melancholia I by Albrect Drer. Reproduced from Reference 2. and sum(diag(D)) produces ans = 34 so the main diagonal is magic. There is no similar command to get the ele- ments of the antidiagonal but there is a command fliplr which produces a left to right mirror image of a matrix. Thus; fliplr(D) produces ans = 13 2 3 16 8 11 10 5 12 7 6 9 1 14 15 4 diag(fliplr(D)) produces ans = 13 11 6 4 and sum(diag(fliplr(D))) produces ans = 34 so we have demonstrated that Drers square is indeed magic. One little thing you might not know; Drer had a good reason for writing the magic square the way he did rather than any of the others that we have produced. He wanted the date he made the engraving to appear in the magic square. The year was 1514. We are going to need to refer to individual elements in a matrix and this is done through subscripts. It is cus- tomary to call the rows i and the columns j and this is written D(i, j ). If i = 4 and j = 3 we are referring to the element in the fourth row of the third column which for Drers matrix D is 14. There is also a way of refer- encing a matrix by the use of a single subscript (normally called k) in which we imagine all the columns standing on top of each other beginning from the left. So instead of D(i, j) or D(4, 3) we can say D(12) and it will return the value 14. There is still plenty of magic left in Drers square. Have a look at the four figures in the top left-hand corner. We can put them in a new matrix with: E = D(1:2, 1:2) This says take the val ues from matrix D which are are in rows one and two, columns one and two and put them in a new matrix, E. The result is: E = 16 3 5 10 Now we can sum them with the commands sum(E) ans = 21 13 sum(E) ans = 21 13 sum(sum(E)) You can guess the answer! There are plenty more sum to 34s to be found; you may enjoy searching for them. So now, even if you knew nothing about matrix algebra before, you know what matrices are and some of the things that can be done with them. So far you will not be able to appreciate just how useful matrix algebra is for solving real problems rather than just manipulating numbers. By the way, we have been looking at integers but this has just been to keep it simple. We would normally expect to be working with floating point numbers. There are two facets of matrix algebra that make it so important. The first is that it enables us to write solutions to prob- lems that would be extremely complex without it. An exampl e being Mahalanobis distance. 3 (It is a long time since we looked at Mahalanobis distance perhaps with our new knowl- edge we can re-visit it!) The other point is that programs such as MAT- LAB make matrix algebra into a very efficient mathematical tool so that computer programs can either be faster or (more likely) do much more com- plex calculations. I hope you will be persuaded next time when we begin to look at our problem. Acknowledgements I am grateful to Tom Fearn for assisting with and checking this article. References 1. A.M.C. Davies, Spectrosc. World 1(1), 30 (1989). 2. Anon., Getting Stated with MAT- LAB. The Math Works, Inc., Natick, MA, USA (1998). 3. S.M. Buco, Spectrosc. World 3(3), 28 (1991). 26 Spectroscopy Europe 12/2 (2000) TONY DAVIESCOLUMN