Posted: Thu Apr 06, 2006 6:35 pm Post subject: VBA to find matrix trace
I have been assigned a problem for class the problem is:
Create a user defined function for the trace of a square matrix. The trace is the sum of the diagonal elements. The input is a range. The output is a double. This function should also test if the number of rows = the number of columns. If not, return the string #ERROR. Because the function can return either a number or a string, the function type should be a variant. The trace is the main diagonal sum.
ex.
4 3 1
5 4 2
6 7 9
The trace is 4 + 4 + 9 = 17
I know how to do everything except get the trace to work in excel.
here is my code so far
Public Function trace(x As Range) As Variant
If x.Rows.Count > x.Columns.Count Or x.Rows.Count < x.Columns.Count Then
trace = "Error"
If x.Rows.Count = x.Columns.Count Then
trace = ????????- here is the part i need help with
End If
End If
End Function
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum