PDA

View Full Version : VBA to determine the rank of a matrix



jnmonroe
08-17-2014, 12:42 PM
Below is a VBA code the Foxes Team developed to determine the rank of a matrix. The VBA is one among several VBA the Foxes Team made available to the public. I would like to be able to make the VBA work as a stand-alone VBA. Sheet 1 in the attached VBA lists 50 measurements (samples) of 4 measurements (column-variables) of the Species Setosa of the Iris Flower Data of Fisher (The use of multi-measurements in taxonomic problems,1931). The fact that the variables are uncorrelated is information we know in advance. Because the variables are uncorrelated the rank of the 50x4 matrix is 4. Sometimes, you don't know in advance that data variables are uncorrelated; in these cases it would help to have a VBA which determines the rank of the matrix. Microsoft's statistical formulas do not include a formula that determines the rank of a matrix.

When I try to run the VBA, a dialog box pops up. The title of the box is "Macro" below this title there is a space expecting me to enter a "Macro Name"

Can anyone suggest what can be done to enable this VBA to run successfully? Ideally, the number "4" should appear somewhere. I know in advance that the rank of the matrix is 4.


Function M_RANK(Mat)
'Returns the rank of a matrix
'ver. 23-6-2002 by L.V.
'Original M_RANK routine by Bernard Wagner, CH (Thanks Bernard)
'
Dim A, At, b, u
Const tiny = 10 ^ -15
A = Mat
N = UBound(A, 1): m = UBound(A, 2) 'get A dimension
'reduce Mat to a square matrix U with the lowest dimension
If N <> m Then
At = M_TRANSP(Mat)
If N < m Then
b = Application.WorksheetFunction.MMult(A, At)
nb = N
Else
b = Application.WorksheetFunction.MMult(At, A)
nb = m
End If
Else
b = A 'nothing to do
nb = N
End If
'compute the sub-space of Ax=0
u = SysLinSing(b, , tiny)
'count null column-vectors of sub-space
Rank = nb
For j = 1 To nb
s = 0: For i = 1 To nb: s = s + Abs(u(i, j)): Next i
If s > tiny Then Rank = Rank - 1
Next j
M_RANK = Rank
End Function

snb
08-17-2014, 01:24 PM
Missing functions

M_transp
SysLinSing

jnmonroe
08-17-2014, 02:04 PM
I appreciate your help.


Missing functions

M_transp
SysLinSing

snb
08-17-2014, 02:37 PM
But I can't without you adding the missing functions/subs.

Paul_Hossler
08-17-2014, 03:57 PM
1. The M_RANK function was in a worksheet module an it should be in a standard module

2. As snb said, there are a number of missing functions and subs so we really can't do any testing

https://www.msu.edu/course/fw/877/bence/matrix_1.8/

3. Not sure how you intend to use it but I added a little to make it look more like a function



Function M_RANK(Mat As Range) As Long
'Returns the rank of a matrix
'ver. 23-6-2002 by L.V.
'Original M_RANK routine by Bernard Wagner, CH (Thanks Bernard)
'
Dim A, At, b, u
Const tiny = 10 ^ -15
A = Mat
N = UBound(A, 1): m = UBound(A, 2) 'get A dimension
'reduce Mat to a square matrix U with the lowest dimension
If N <> m Then
At = M_TRANSP(Mat)
If N < m Then
b = Application.WorksheetFunction.MMult(A, At)
nb = N
Else
b = Application.WorksheetFunction.MMult(At, A)
nb = m
End If
Else
b = A 'nothing to do
nb = N
End If
'compute the sub-space of Ax=0
u = SysLinSing(b, , tiny)
'count null column-vectors of sub-space
Rank = nb
For j = 1 To nb
s = 0: For i = 1 To nb: s = s + Abs(u(i, j)): Next i
If s > tiny Then Rank = Rank - 1
Next j
M_RANK = Rank
End Function



4. And put an example call on the data sheet

jnmonroe
08-19-2014, 07:45 PM
Paul,

Thank you for your help.

I changed the values of column D to be a constant multiple of the values of column C and the rank changed from 4 to 3 as it should.

I added a 5th column (column E) and changed the function you placed in G5 from =m_rank($A$2:$DE$51) to =m_rank($A$2:$E$51).

When I ran the code a compile error, function or sub-function not defined, highlighted at the first line, Function M_RANK(Mat As Range) As Long.

Do you know what the problem was with my trying to add the 5th column and changing to function for it to output the rank of the matrix after the 5th column was added?

Thanks again,

John

Paul_Hossler
08-20-2014, 10:17 AM
Since the subs and functions that M_RANK calls are not in the workbook, I really can't even run the code. I don't think adding the 5th col is the issue

JUST A GUESS ... Is the M_RANK subroutine in the same workbook as the data and is it in a standard module?

jnmonroe
08-20-2014, 07:44 PM
Paul,

Appreciate the additional comments. I'll continue to work on it.

John