I need help writing code in VB to determine the the number of independent rows(rank) of a matrix. You can use any N x N matrix. I have an idea of what I want to do but I'm new to VB and struggle putting it into code that works. Please help!
I need help writing code in VB to determine the the number of independent rows(rank) of a matrix. You can use any N x N matrix. I have an idea of what I want to do but I'm new to VB and struggle putting it into code that works. Please help!
Ask and ye shall receive. Attached is the file to use. Code is unlocked. You can enter any matrix and it will find the number of non-zero rows. Since you are learning VB, I put comments in the code for you for each step of the way. Run-time is lightning fast. Be advised this does not solve the matrix, or reduce rows. If that is what you want, give me some more detail on what you want to do. You would need this code anyway if you want to take it further.
For those users who want to learn code, but don't know their way around vb, I'm pasting in the full code on this post:
Sub findrank()
' findrank macro recorded by Math Celebrity
' Declare variables
Dim a, b, i, j, rowtot, ranktot As Integer
' Error message to user if first cell not filled out for the matrix
If Len(Range("A1").Value) = 0 Then
MsgBox "You need to enter your first matrix value in Cell A1", vbExclamation, "Missing Information"
Range("A1").Select
Exit Sub
End If
' Start at the upper left corner of Excel for your matrix evaluation
' Find number of rows of the matrix
a = 1
Do While Len(Cells(a, 1)) > 0
a = a + 1
Loop
' Find number of columns of the matrix
b = 1
Do While Len(Cells(1, b)) > 0
b = b + 1
Loop
' Now loop through every column of each row, and check to see if all entries are zero
ranktot = 0
rowtot = 0
For i = 1 To a - 1
' reset row tot for each new row
rowtot = 0
For j = 1 To b - 1
If Cells(i, j).Value <> 0 Then rowtot = rowtot + 1
Next j
'For every row that has non-zero entries, increase rank by 1
If rowtot > 0 Then ranktot = ranktot + 1
Next i
' Message Box answer of rank to user
MsgBox "The rank of your " & i - 1 & " x " & j - 1 & " matrix = " & ranktot, vbInformation, "Matrix Rank"
End Sub
You've got the rank part from what I did. You need to write the Gauss part.
Looking at my code, are you comfortable with how to loop through matrix entries? It sometimes helps to write out each point in the loop on paper.
For example, when row is 1, column is 1, then 2, then 3, then 4 then ...
Then row is 1, column is 1,2,3,4...
You could post your code here if you want for assistance. Else, take a look at your overflow and see what happened.
Did you declare an integer and now it's a double result value? That's a common overflow error. Did you declare an array and you go outside the max index or below the min index?
For instance, if you declare Dim newarray(1 to 50) and assign a value like newarray(0), that's a problem.
If you declare Dim mynumber As integer, and then do mynumber = 0.8*.6, that could be an overflow as well.
God I hate VBA... (Overflow error question) - VBForums