Results 1 to 7 of 7

Math Help - independence and rank of matrix with VBA

  1. #1
    Newbie
    Joined
    Apr 2008
    Posts
    2

    independence and rank of matrix with VBA

    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!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Feb 2008
    From
    Berkeley, Illinois
    Posts
    364
    Quote Originally Posted by Duf88gtr View Post
    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
    Attached Files Attached Files
    Last edited by mathceleb; May 7th 2008 at 05:15 PM.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Apr 2008
    Posts
    2

    gauss elimination

    im new at vb. i dont know how to take a matrix and write a vb code to compute the gauss elimination or gauss-jordan method. and then use that to find the ranking.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Senior Member
    Joined
    Feb 2008
    From
    Berkeley, Illinois
    Posts
    364
    Quote Originally Posted by mohit mathavan View Post
    im new at vb. i dont know how to take a matrix and write a vb code to compute the gauss elimination or gauss-jordan method. and then use that to find the ranking.
    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...
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Apr 2008
    Posts
    2
    i tried writing a gauss code for 6X6 matrix but it gave me overflow.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Senior Member
    Joined
    Feb 2008
    From
    Berkeley, Illinois
    Posts
    364
    Quote Originally Posted by mohit mathavan View Post
    i tried writing a gauss code for 6X6 matrix but it gave me overflow.
    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
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Apr 2008
    Posts
    1
    mohit mathavan!

    If you still need the code, send me your email.

    Quote Originally Posted by mathceleb View Post
    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
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Rank of a matrix
    Posted in the Advanced Algebra Forum
    Replies: 2
    Last Post: November 30th 2010, 06:40 PM
  2. Replies: 3
    Last Post: August 20th 2010, 06:32 AM
  3. Column rank, linear independence and RRE form
    Posted in the Advanced Algebra Forum
    Replies: 12
    Last Post: April 11th 2010, 09:38 AM
  4. Replies: 1
    Last Post: April 1st 2010, 09:34 AM
  5. Rank of this 3x4 matrix?
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: March 24th 2010, 12:05 AM

Search Tags


/mathhelpforum @mathhelpforum