# Introduction

This example will look at a simple 4 by 4 magic square, shown below. Logical indexing and the sum function (with a single input argument) will be used to look at the sum of each row, each column, the diagonal and antidiagonal in the magic square. For this you need to be comfortable with running Octave/MATLAB and need to understand how to measure the size of an array and use logical indexing. See my guide Introduction to Arrays in Octave and MATLAB if you need a refresher.

As computing the sum of rows for example is an operation being carried out

for

a sequence of events, it can be automated by using a

for

loop.

Note much of the steps below can be simplified by inputting multiple input arguments into the

sum

function and using the

<span style="color: #3366ff;">diag</span>

,

<span style="color: #3366ff;">flip,</span>
<span style="color: #3366ff;">flipud</span>

and

<span style="color: #3366ff;">fliplr</span>

functions however the main point of this written guide is to introduce the powerful tool of

<span style="color: #3366ff;">for</span>

loops using a simple example.

# Create a Magic Square

Working with a Magic Square is a good way to practice working with logical indexing of arrays and hence explore the concept behind creating a for loop. A Magic Square can be created using the

magic

function, the input argument will be the size of the square, in this case I will use 4:

<span style="color: #339966;">% The magic</span> <span style="color: #339966;">square</span>

M=<span style="color: #3366ff;">magic(</span>4<span style="color: #3366ff;">)</span>

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

# Look at a Matrix Dimensions

Although we know the size of the square is 4 by 4, let's use the size function to prescribe the magic squares dimensions to the variables m (number of rows) and n (number of columns).

<span style="color: #ff0000;">[</span>m,n<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">size(</span>M<span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=4\end{array}$

# Initialisation of Output Array – sumrow

To begin we should initialise our column vector which contains a column of sums for each row. This can be done using the

zeros

function, the inputs for the zero function are m and 1 as we are looking for a column vector that has m rows (and 1 column)

sumrow=<span style="color: #3366ff;">zeros(</span>m,1<span style="color: #3366ff;">)</span>

$\displaystyle \text{sumrow}=\left[ {\begin{array}{*{20}{c}} 0 \\ 0 \\ 0 \\ 0 \end{array}} \right]$

Initialisation in essence, creates an array with the desired variable name and desired final dimensions. In this case the column of sums which is going to be m rows by 1 column. At initialisation we don't know the values of each element in the array so we prescribe each value to 0. We can sub-sequentially use logical indexing to replace each of the values in this column vector.

# Look at the Sum of the First Row

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

Let's look at the first row:

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">1</span>,<span style="color: #ff9900;">[</span><span style="color: #000000;">1</span><span style="color: #ff9900;"><span style="color: #000000;">:n</span>]</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {16+2+3+13} \\ 0 \\ 0 \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ 0 \\ 0 \\ 0 \end{array}} \right]\end{array}$

This can also be abbreviated as:

<span style="color: #666699;">sumrow(</span><strong><span style="color: #ff0000;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">1</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

What we are essentially doing on the left hand side is using logical indexing to prescribe the value to the 1st index of the column vector sumrow. To the right hand side we are using logical indexing to create a row vector and inputting this row vector into the sum function. The scalar calculated from the sum function is prescribed to the 1st index of the column vector sumrow.

# Look at the Sum of Each Row

We can now manually go through the same process by looking at each row in turn.

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>1</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">1</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {16+2+3+13} \\ 0 \\ 0 \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ 0 \\ 0 \\ 0 \end{array}} \right]\end{array}$

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>2</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">2</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {5+11+10+8} \\ 0 \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ 0 \\ 0 \end{array}} \right]\end{array}$

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">3</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {9+7+6+12} \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {34} \\ 0 \end{array}} \right]\end{array}$

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>4</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">4</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {34} \\ {4+14+15+1} \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {34} \\ {34} \end{array}} \right]\end{array}$

# Look at the Sum of Each Row in a For Loop

Note how we used the following lines of code, these all look identical with exception to the part highlighted in red

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>1</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">1</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>2</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">2</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">3</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>4</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">4</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

These can be combined together using a for loop. The for loop has to start withÂ

<span style="color: #3366ff;">for</span>

Â followed by some

<span style="color: #ff9900;">condition</span>

Â which increments from a starting point in steps until an ending point. We useÂ

<span style="color: #3366ff;">end</span>

Â to end the for loop.

<span style="color: #3366ff;">for <span style="color: #ff9900;">condition</span></span>

<span style="color: #3366ff;">end</span>

We need to create a condition that increments during each iteration of the for loop, let's call it A. From the above, the only change in the line of code is the value in red, we need to set this to A and have A to equal the value shown above for each iteration of a

<span style="color: #3366ff;">for</span>

loop. Thus we know that A starts at 1, then steps up by increments of 1 to 2, 3 and ends at m which in this case is 4. This condition is met whenÂ

A=<span style="color: #ff0000;">[</span>1:m<span style="color: #ff0000;">]'</span>

, the transpose is used because we are looking for a column vector, containing the sum value of each row.

<span style="color: #3366ff;">for</span>Â <span style="color: #000000;"><strong><span style="color: #ff0000;">A</span></strong>=<span style="color: #ff0000;">[</span>1:m</span><span style="color: #ff0000;"><span style="color: #000000;"><span style="color: #ff0000;">]'</span></span></span>

<span style="color: #3366ff;">end</span>

Now we can rewrite:

<span style="color: #666699;">sumrow(</span><strong>1</strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">1</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumrow(</span><strong>2</strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">2</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumrow(</span><strong>3</strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">3</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumrow(</span><strong>4</strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">4</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

As a single line in terms of A:

<span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>A</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">A</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

To the left hand side we are embedding into the Ath index of the column vector sumrow. To the right hand side we are calculating the sum of each row of M.

This line of code should be embedded within the for loop. The convention is to indent any code embedded in a loop therefore it will have the form:

<span style="color: #3366ff;">for</span>Â  <span style="color: #000000;"><span style="color: #ff0000;"><strong>A</strong></span>=<span style="color: #ff0000;">[</span>1:m</span><span style="color: #ff0000;"><span style="color: #000000;"><span style="color: #ff0000;">]'</span></span></span>

Â  Â  <span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>A</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">A</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

sumrow=<span style="color: #3366ff;">zeros(</span>m,1<span style="color: #3366ff;">)</span>

$\displaystyle \text{sumrow}=\left[ {\begin{array}{*{20}{c}} 0 \\ 0 \\ 0 \\ 0 \end{array}} \right]$

The code

<span style="color: #3366ff;">for</span>Â <span style="color: #000000;"><strong><span style="color: #ff0000;">A</span></strong>=<span style="color: #ff0000;">[</span>1:m<span style="color: #ff0000;">]'</span></span>

Â  Â  <span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>A</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">A</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

In the first step will become

<span style="color: #3366ff;">for</span>Â <span style="color: #000000;"><span style="color: #ff0000;"><strong>A</strong><span style="color: #000000;">=</span><strong>1</strong></span></span>

Â  Â  <span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>1</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">1</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {16+2+3+13} \\ 0 \\ 0 \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ 0 \\ 0 \\ 0 \end{array}} \right]\end{array}$

In the second step will become

<span style="color: #3366ff;">for</span>Â <span style="color: #000000;"><strong><span style="color: #ff0000;">A</span></strong>=<span style="color: #ff0000;"><strong>2</strong></span></span>

Â  Â  <span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>2</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">2</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {5+11+10+8} \\ 0 \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ 0 \\ 0 \end{array}} \right]\end{array}$

In the third step it will become

<span style="color: #3366ff;">for</span>Â <span style="color: #ff0000;"><strong>A</strong><span style="color: #000000;">=</span><strong>3</strong></span>

Â  Â  <span style="color: #666699;">sumrow(</span><span style="color: #ff0000;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">3</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {9+7+6+12} \\ 0 \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {34} \\ 0 \end{array}} \right]\end{array}$

In the mth step, it will become

<span style="color: #ff0000;"><span style="color: #3366ff;">forÂ </span><strong>A</strong><span style="color: #000000;">=</span><strong>m</strong></span>

Â  Â <span style="color: #666699;"> sumrow(</span><strong><span style="color: #ff0000;">m</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">m</span></strong>,<span style="color: #ff9900;">:</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {34} \\ {4+14+15+1} \end{array}} \right]\\\text{sumrow}=\left[ {\begin{array}{*{20}{c}} {34} \\ {34} \\ {34} \\ {34} \end{array}} \right]\end{array}$

This gives us the m by 1 column vector of the sums of the rows of the magic square respectively.

# Initialisation of Output Array – sumcol

Now we can follow a similar procedure. This time looking at the sums of each column which will give us a row vector called sumcol. To begin we should initialise our row vector which contains a row of sums for each column. This can be done using the

<span style="color: #3366ff;">zeros</span>

function, the inputs for the zero function are 1 and n as we are looking for a row vector that has (1 row and) m columns

sumrow=<span style="color: #3366ff;">zeros(</span>1,n<span style="color: #3366ff;">)</span>

$\displaystyle \text{sumcol}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 \end{array}} \right]$

Initialisation in essence, creates an array with the desired variable name and desired final dimensions. In this case the column of sums which is going to be m rows by 1 column. At initialisation we don't know the values of each element in the array so we prescribe each value to 0. We can sub-sequentially use logical indexing to replace each of the values in this column vector.

# Look at the Sum of the First Column

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

Let's look at the first column

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(<span style="color: #ff0000;">[</span></span><span style="color: #ff0000;"><span style="color: #000000;">1:m</span>]'</span>,<strong><span style="color: #ff9900;">1</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} \begin{array}{l}16\\+\\5\\+\\9\\+\\4\end{array} & 0 & 0 & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & 0 & 0 & 0 \end{array}} \right]\end{array}$

This can also be abbreviated as:

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">1</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

What we are essentially doing on the left hand side is using logical indexing to prescribe the value to the 1st index of the row vector sumcol. To the right hand side we are using logical indexing to create a column vector and inputting this column vector into the sum function. The scalar calculated from the sum function is prescribed to the 1st index of the row vector sumcol.

# Look at the Sum of Each Column

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">1</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} \begin{array}{l}16\\+\\5\\+\\9\\+\\4\end{array} & 0 & 0 & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & 0 & 0 & 0 \end{array}} \right]\end{array}$

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">2</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">2</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & \begin{array}{l}2\\+\\11\\+\\7\\+\\14\end{array} & 0 & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & 0 & 0 \end{array}} \right]\end{array}$

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">3</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">3</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & \begin{array}{l}2\\+\\11\\+\\7\\+\\14\end{array} & 0 & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & 0 & 0 \end{array}} \right]\end{array}$

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">4</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">4</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & {34} & \begin{array}{l}13\\+\\8\\+\\12\\+\\1\end{array} \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & {34} & {34} \end{array}} \right]\end{array}$

# Look at the Sum of Each Column using a For Loop

Note how we used the following lines of code, these all look identical with exception to the part highlighted in orange

<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">1</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">2</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">2</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">3</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">3</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">sumcol(</span><strong><span style="color: #ff9900;">4</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><span style="color: #ff0000;">:</span>,<strong><span style="color: #ff9900;">4</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

These can be combined together using a for loop. Once again we need to create a condition which has some starting value and increments in steps until a final value, let's call it B. We know that B starts at 1, then steps up by increments of 1 to 2, 3 and ends at 4. In other words the row vectorÂ

<span style="color: #ff9900;">B</span>=<span style="color: #ff9900;">[</span>1:n<span style="color: #ff9900;">]</span>

. We use a row vector as we are looking to create a row vector which contains the sum of each columns. Therefore the for loop becomes:

<span style="color: #3366ff;">for</span>Â <strong><span style="color: #ff9900;">B</span></strong>=<span style="color: #ff9900;">[</span>1:n<span style="color: #ff9900;">]</span>

Â  Â  <span style="color: #666699;">sumcol(</span><span style="color: #ff9900;"><strong>B</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">:</span></strong>,<strong><span style="color: #ff9900;">B</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

To the left hand side we are embedding into the Bth index of the row vector sumcol. To the right hand side we are calculating the sum of each col of M. This line of code should once again be embedded within the for loop. Once again we follow the convention is to indent any code embedded in a loop.

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

sumcol=<span style="color: #3366ff;">zeros(</span>1,n<span style="color: #3366ff;">)</span>

$\displaystyle \text{sumcol}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 \end{array}} \right]$

The code

<span style="color: #3366ff;">for</span>Â <strong><span style="color: #ff9900;">B</span></strong><span style="color: #000000;">=</span><span style="color: #ff9900;">[</span><span style="color: #000000;">1:n</span><span style="color: #ff9900;">]</span>

Â  Â  <span style="color: #666699;">sumcol(</span><span style="color: #ff9900;"><strong>B</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">:</span></strong>,<strong><span style="color: #ff9900;">B</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

In the first step will become

<span style="color: #3366ff;">for</span>Â <strong style="color: #800000;"><span style="color: #ff9900;">B</span></strong><span style="color: #000000;">=</span><span style="color: #ff9900;"><strong>1</strong></span>

Â  Â  <span style="color: #666699;">sumcol(</span><span style="color: #ff9900;"><strong>1</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">:</span></strong>,<strong><span style="color: #ff9900;">1</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} \begin{array}{l}16\\+\\5\\+\\9\\+\\4\end{array} & 0 & 0 & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & 0 & 0 & 0 \end{array}} \right]\end{array}$

In the second step will become

<span style="color: #3366ff;">for</span>Â <strong style="color: #800000;"><span style="color: #ff9900;">B</span></strong><span style="color: #000000;">=</span><strong><span style="color: #ff9900;">2</span></strong>

Â  Â  <span style="color: #666699;">sumcol(</span><span style="color: #ff9900;"><strong>2</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">:</span></strong>,<strong><span style="color: #ff9900;">2</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & \begin{array}{l}2\\+\\11\\+\\7\\+\\14\end{array} & 0 & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & 0 & 0 \end{array}} \right]\end{array}$

In the third step will become

<span style="color: #3366ff;">for</span>Â <strong style="color: #800000;"><span style="color: #ff9900;">B</span></strong><span style="color: #000000;">=</span><strong><span style="color: #ff9900;">3</span></strong>

Â  Â  <span style="color: #666699;">sumcol(</span><span style="color: #ff9900;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">:</span></strong>,<strong><span style="color: #ff9900;">3</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & \begin{array}{l}3\\+\\10\\+\\6\\+\\15\end{array} & 0 \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & {34} & 0 \end{array}} \right]\end{array}$

In the nth step it will become

<span style="color: #3366ff;">for</span>Â <strong style="color: #800000;"><span style="color: #ff9900;">B</span></strong><span style="color: #000000;">=</span><span style="color: #ff9900;"><strong>n</strong></span>

Â  Â  <span style="color: #666699;">sumcol(</span><span style="color: #ff9900;"><strong>n</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">sum(</span><span style="color: #00ccff;">M(</span><strong><span style="color: #ff0000;">:</span></strong>,<strong><span style="color: #ff9900;">n</span></strong><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \begin{array}{l}\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & {34} & \begin{array}{l}13\\+\\8\\+\\12\\+\\1\end{array} \end{array}} \right]\\\text{sumcol}=\left[ {\begin{array}{*{20}{c}} {34} & {34} & {34} & {34} \end{array}} \right]\end{array}$

This gives us the 1 by n row vector of the sums of the columns of the magic square respectively.

# Initialisation of Output Array – diag

It may not be immediately obvious but we can use the same method to look at the sum of the diagonal. To do this we need to turn the diagonal into a row (or column) vector, we will use a row vector.

Let's first initialise the row vector diag using

diag=<span style="color: #3366ff;">zeros(</span>1,n<span style="color: #3366ff;">)</span>

$\displaystyle \text{diag}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 \end{array}} \right]$

# Create diag row vector using a For Loop

Manually the diagonal has the position

<span style="color: #666699;">diag(</span><strong><span style="color: #ffcc00;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">1</span></strong>,<strong><span style="color: #ffcc00;">1</span></strong><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;"><span style="color: #666699;">diag(</span></span><strong><span style="color: #ffcc00;">2</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">2</span></strong>,<strong><span style="color: #ffcc00;">2</span></strong><span style="color: #3366ff;">)</span>
<span style="color: #666699;">diag(</span><strong><span style="color: #ffcc00;">3</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">3</span></strong>,<strong><span style="color: #ffcc00;">3</span></strong><span style="color: #3366ff;">)</span>
<span style="color: #666699;">diag(</span><strong><span style="color: #ffcc00;">4</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">4</span></strong>,<strong><span style="color: #ffcc00;">4</span></strong><span style="color: #3366ff;">)</span>

We can create the above by using the

<span style="color: #3366ff;">for</span>

loop. We need to create a condition which has some starting value and increments in steps until an end value, let's call it C. We know that C starts at 1, then steps up by increments of 1 to 2, 3 and ends at n=4 in this case. In other words the column vectorÂ

<span style="color: #ffcc00;">C</span>=<span style="color: #ffcc00;">[</span>1:n<span style="color: #ffcc00;">]</span>

. Therefore the for loop becomes:

<span style="color: #3366ff;">for</span> <strong><span style="color: #ffcc00;">C</span></strong>=1:n
Â  Â  Â <span style="color: #666699;">diag(</span><strong><span style="color: #ffcc00;">C</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">C</span></strong>,<strong><span style="color: #ffcc00;">C</span></strong><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

To the left hand side we are embedding into the Cth index of the row vector diag. To the right hand side we are indexing the Cth index into both the row and column. This line of code should once again be embedded within the for loop. Recalling the convention is to indent any code embedded in a loop.

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

diag=<span style="color: #3366ff;">zeros(</span>1,n<span style="color: #3366ff;">)</span>

$\displaystyle \text{diag}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 \end{array}} \right]$

The code

<span style="color: #3366ff;">for</span> <strong><span style="color: #ffcc00;">C</span></strong>=1:n
Â  Â  Â <span style="color: #3366ff;">diag(</span><strong><span style="color: #ffcc00;">C</span></strong><span style="color: #3366ff;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">C</span></strong>,<strong><span style="color: #ffcc00;">C</span></strong><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

In the first step will become

<span style="color: #3366ff;">for</span> <strong><span style="color: #ff9900;">C</span></strong>=<span style="color: #ff0000;"><strong><span style="color: #ff9900;">1</span></strong></span>
Â  Â  Â <span style="color: #666699;">diag(</span><strong><span style="color: #ff9900;">1</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ff9900;">1</span></strong>,<strong><span style="color: #ff9900;">1</span></strong><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \text{diag}=\left[ {\begin{array}{*{20}{c}} {16} & 0 & 0 & 0 \end{array}} \right]$

In the second step will become

<span style="color: #3366ff;">for</span> <strong><span style="color: #ff9900;">C</span></strong>=<span style="color: #ff0000;"><span style="color: #ff9900;"><strong>2</strong></span></span>
Â  Â  <span style="color: #666699;">Â diag(</span><strong><span style="color: #ff9900;">2</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ff9900;">2</span></strong>,<strong><span style="color: #ff9900;">2</span></strong><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \text{diag}=\left[ {\begin{array}{*{20}{c}} {16} & {11} & 0 & 0 \end{array}} \right]$

In the third step will become

<span style="color: #3366ff;">for</span> <strong><span style="color: #ff9900;">C</span></strong>=<span style="color: #ff0000;"><strong><span style="color: #ff9900;">3</span></strong></span>
Â  Â  Â <span style="color: #666699;">diag(</span><span style="color: #ff9900;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ff9900;">3</span></strong>,<span style="color: #ff9900;"><strong>3</strong></span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \text{diag}=\left[ {\begin{array}{*{20}{c}} {16} & {11} & 6 & 0 \end{array}} \right]$

In the nth step will become

<span style="color: #3366ff;">for</span> <strong><span style="color: #ffcc00;">C</span></strong>=<span style="color: #ffcc00;"><strong>n</strong></span>
Â  Â  Â <span style="color: #666699;">diag(</span><span style="color: #ffcc00;"><strong>n</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><strong><span style="color: #ffcc00;">n</span></strong>,<strong><span style="color: #ffcc00;">n</span></strong><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \text{diag}=\left[ {\begin{array}{*{20}{c}} {16} & {11} & 6 & 1 \end{array}} \right]$

# Calculate the sum of the diag

Now that we have the diag as a row vector, calculating the sum of it is fairly straight forward.

sumdiag=<span style="color: #3366ff;">sum(</span>diag<span style="color: #3366ff;">)</span>

$\displaystyle 34$

# Initialisation of Output Array – antidiag

We can use the same method to look at the sum of the antidiagonal. To do this we need to turn the antidiagonal into a row (or column) vector, we will use a row vector.

Let's first initialise the row vector diag using

antidiag=<span style="color: #3366ff;">zeros(</span>1,n<span style="color: #3366ff;">)</span>

$\displaystyle \text{antidiag}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 \end{array}} \right]$

# Create antidiag row vector using a For Loop

Manually the diagonal has the position

<span style="color: #666699;">antidiag(</span><span style="color: #ff0000;"><strong>1</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>1</strong></span>,<span style="color: #ff9900;"><strong>4</strong></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;"><span style="color: #666699;">antidiag(</span></span><span style="color: #ff0000;"><strong>2</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>2</strong></span>,<span style="color: #ff9900;"><strong>3</strong></span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">antidiag(</span><span style="color: #ff0000;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>3</strong></span>,<span style="color: #ff9900;"><strong>2</strong></span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">antidiag(</span><span style="color: #ff0000;"><strong>4</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>4</strong></span>,<span style="color: #ff9900;"><strong>1</strong></span><span style="color: #3366ff;">)</span>

We can create the above by using the

<span style="color: #3366ff;">for</span>

loop. We need to create a condition which has some starting value and increments in steps to a final value, let's call it D. We know that D starts at 1, then steps up by increments of 1 to 2, 3 and ends at n=4 in this case. In other words the column vectorÂ

<span style="color: #ffcc00;">D</span>=<span style="color: #ffcc00;">[</span>1:n<span style="color: #ffcc00;">]</span>

. It is obvious how to calculate the index on the left hand side and the row index on the right hand side in terms of D but in order to make a loop we also need to define the column index in terms of D. We have the size of the matrix as m by n and we know we are lowering our index as D increases. If we use n-D, this will give us 3,2,1,0 however what we want is 4,3,2,1 therefore we must use n+1-D.Â The +1 is required when indexing the column as we index the matrix from 1 upwards and not from 0 upwards.

<span style="color: #666699;">antidiag(</span><span style="color: #ff0000;"><strong>1</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>1</strong></span>,<span style="color: #ff9900;"><strong>n+1-<span style="color: #ff0000;">1</span></strong></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;"><span style="color: #666699;">antidiag(</span></span><span style="color: #ff0000;"><strong>2</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>2</strong></span>,<span style="color: #ff9900;"><strong>n+1-<span style="color: #ff0000;">2</span></strong></span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">antidiag(</span><span style="color: #ff0000;"><strong>3</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>3</strong></span>,<span style="color: #ff9900;"><strong>n+1-<span style="color: #ff0000;">3</span></strong></span><span style="color: #3366ff;">)</span>
<span style="color: #666699;">antidiag(</span><span style="color: #ff0000;"><strong>n</strong></span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;"><strong>n</strong></span>,<span style="color: #ff9900;"><strong>n+1-<span style="color: #ff0000;">n</span></strong></span><span style="color: #3366ff;">)</span>

Therefore the for loop becomes

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=1:n
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">D</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">D</span>,<span style="color: #ff9900;">n+1-</span><span style="color: #ff0000;">D</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

To the left hand side we are embedding into the Dth index of the row vector diag. To the right hand side we are indexing the Dth index into the row. For the antidiag, when the row=1, the column=end and when the row=end, the column=1. Introducing a variable D=1:n we know that the antidiag lies on M(D,n-D+1). This line of code should once again be embedded within the for loop. Once again we follow the convention to indent any code embedded in a loop. When running this code, we will therefore start with the following

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]$

$\displaystyle \text{antidiag}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 \end{array}} \right]$

The code

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=1:n
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">D</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">D</span>,<span style="color: #ff9900;">n+1-</span><span style="color: #ff0000;">D</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

In the first step will become

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">1</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">1</span>,<span style="color: #ffcc00;">n+1-</span><span style="color: #ff0000;">1</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

Which gives the first element of the antidiagonal (in the first row)

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">1</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">1</span>,<span style="color: #ffcc00;">n</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \left[ {\begin{array}{*{20}{c}} {13} & 0 & 0 & 0 \end{array}} \right]$

In the second step will become

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">2</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">2</span>,<span style="color: #ff9900;">n+1-</span><span style="color: #ff0000;">2</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

Which gives the second element of the antidiagonal (in the second row)

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">2</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">2</span>,<span style="color: #ff9900;">n-1</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \left[ {\begin{array}{*{20}{c}} {13} & {10} & 0 & 0 \end{array}} \right]$

In the third step will become

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">3</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">3</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">3</span>,<span style="color: #ffcc00;"><span style="color: #ff9900;">n+1-</span></span><span style="color: #ff0000;">3</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

Which gives the third element of the antidiagonal (in the third row)

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">3</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">3</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">3</span>,<span style="color: #ff9900;">n-2</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \left[ {\begin{array}{*{20}{c}} {13} & {10} & 7 & 0 \end{array}} \right]$

In the nth step will become

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">n</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">n</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">n</span>,<span style="color: #ff9900;">n+1-</span><span style="color: #ff0000;">n</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

However

n=end

, Which gives the last element of the antidiagonal

<span style="color: #3366ff;">for</span> <span style="color: #ff0000;">D</span>=<span style="color: #ff0000;">n</span>
Â  Â  Â <span style="color: #666699;">antidiag(</span><span style="color: #ff0000;">n</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">M(</span><span style="color: #ff0000;">n</span>,<span style="color: #ff9900;">1</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

$\displaystyle \left[ {\begin{array}{*{20}{c}} {13} & {10} & 7 & 4 \end{array}} \right]$

# Calculate the sum of the diag

Now that we have the antidiag as a row vector, calculating the sum of it is fairly straight forward.

sumantidiag=<span style="color: #3366ff;">sum(</span>antidiag<span style="color: #3366ff;">)</span>

$\displaystyle 34$

# Script to Measure Sum of All Sides of a Magic Square

[code language="matlab"]
% Create a Magic Square
M=magic(4)
% Get the Size of the Magic Square
[m,n]=size(M)
%
% The sums of the rows
% Initialisation of the column vector sumrow
sumrow=zeros(m,1);
% For Loop Measuring the sums of the rows
for A=[1:m]'
sumrow(A)=sum(M(A,:));
end
sumrow
%
% The sums of the cols
% Initialisation of the row vector sumcol
sumcol=zeros(1,n);
% For Loop Measuring the sums of the columns
for B=1:n
sumcol(B)=sum(M(B,:));
end
sumcol
%
% The sum of the diagonal
% Initialisation of the diagonal as a row vector
diag=zeros(1,n);
% For Loop computing the diagonal as a row vector
for C=1:n
diag(C)=M(C,C);
end
% The sum of the diagonal
sumdiag=sum(diag)
%
% The sum of the antidiagonal
% Intialisation of the antidiagonal as a row vector
antidiag=zeros(1,n);
% For Loop computing the antidiagonal as a row vector
for D=1:n
antidiag(D)=M(D,n+1-D);
end
% The sum of the antidiagonal
sumantidiag=sum(antidiag)
[/code]

We used a relatively small Magic Square that was 4 by 4 where it is possible to visually manually calculate the sum of the rows, the sum of the columns and the sum of the diagonals i.e.

$\displaystyle \begin{array}{*{20}{c}} {} & {} & {} & {} & {} & {34} \\ {} & {16} & 2 & 3 & {13} & {34} \\ {} & 5 & {11} & {10} & 8 & {34} \\ {} & 9 & 7 & 6 & {12} & {34} \\ {} & 4 & {14} & {15} & 1 & {34} \\ {} & {34} & {34} & {34} & {34} & {34} \end{array}$

We can however change the code above to look at a far more complicated 9 by 9 magic square by changing the 4 in line 2 to a 9: