Sunday, February 21, 2016

Matrixes: Trace of a square matrix


All the elements presented are not warranted to be correct or free from defects. 
Please report any errors found to  afstblogs@gmail.com

The trace of a square matrix is the sum of the diagonal elements of the matrix.




Function xlMtxTrace(Mtx)
    
    ' Mtx - Matrix for which the trace will be computed
    ' If the matrix is not square the funtion returns the message
    '    m<>n
    
    m = Mtx.Rows.Count
    n = Mtx.Columns.Count
    
    If m <> n Then
        xlMtxTrace = "m<>n"
    Else
        For i = 1 To n
            xlMtxTrace = xlMtxTrace + Mtx(i, i)
        Next i
    End If
    
End Function