matrix structures problem

moonpecs

New member
Joined
Jul 29, 2011
Messages
4
Programming Experience
Beginner
im writing a code in matrix form for my numerical subject...please i badly needed your help guys...i cant find the correct code for the matrix pattern shown below..
the code can simply be written by using a for loop only for that matrix size (shown below) and it has a 14 x 14...but i want to write a general code that is applicable to any size...and my actual problem is 2000 by 2000 matrix.....


0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
i cant find the correct code for the matrix pattern shown below..
the code can simply be written by using a for loop only for that matrix size (shown below) and it has a 14 x 14...but i want to write a general code that is applicable to any size...and my actual problem is 2000 by 2000 matrix.....

What do you mean you can't find the correct code? I'm not sure if i understand what the problem is exactly? But if your just trying to create that pattern you can just use a for loop,a multi-dimension array, and some variables. Maybe you can create a class for this matrix object if that's what you need?


To make the pattern with just a for loop its pretty simple, you can do something like this:

(please note i wrote this code real quick as an example, i feel like there is a small bug with the logic but it does outputs what is shown below, and this should give you an idea of how to do it if this is what your having trouble with?)
        Dim intMatrix(19, 19) As Integer

        If intMatrix.GetUpperBound(1) > 0 AndAlso intMatrix.GetUpperBound(0) > 0 Then
            Dim count As Integer = 0
            Dim NumberCount As Integer = 1
            Dim col As Integer = intMatrix.GetUpperBound(1) - 1

            For row As Integer = intMatrix.GetUpperBound(0) To 0 Step -1
                If count > 0 Then
                    intMatrix(row, col) += 1
                    col -= 1
                ElseIf count = 0 Then
                    NumberCount += 1
                    count = NumberCount
                End If

                count -= 1
            Next
        End If


To change the size, just change the size of the array.

example output from the code above (20x20):
VB.NET:
00001000000000000000
00000100000000000000
00000010000000000000
00000001000000000000
00000000100000000000
00000000000000000000
00000000010000000000
00000000001000000000
00000000000100000000
00000000000010000000
00000000000000000000
00000000000001000000
00000000000000100000
00000000000000010000
00000000000000000000
00000000000000001000
00000000000000000100
00000000000000000000
00000000000000000010
00000000000000000000

if you change the size to a 14x14 you get:
VB.NET:
00010000000000
00001000000000
00000100000000
00000010000000
00000000000000
00000001000000
00000000100000
00000000010000
00000000000000
00000000001000
00000000000100
00000000000000
00000000000010
00000000000000
a 5x7
VB.NET:
10000
00000
01000
00100
00000
00010
00000

and a 2x2 gets u:
VB.NET:
10
00
 
writing a code in matrix

thank you very much Flippedbeyond.....
by the way, how do you make output??? Im new to programming and VBasic..............

im using a "TextBox1.AppendText(intMatrix(row, col) & vbTab)".....the output is al zeros!!!!
or
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

or I did something like this:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim intMatrix(19, 19) As Integer

If intMatrix.GetUpperBound(1) > 0 AndAlso intMatrix.GetUpperBound(0) > 0 Then
Dim count As Integer = 0
Dim NumberCount As Integer = 1
Dim col As Integer = intMatrix.GetUpperBound(1) - 1

For row As Integer = intMatrix.GetUpperBound(0) To 0 Step -1
If count > 0 Then
intMatrix(row, col) += 1
col -= 1
ElseIf count = 0 Then
NumberCount += 1
count = NumberCount

End If

count -= 1
TextBox1.AppendText(intMatrix(row, col) & vbTab)
Application.DoEvents()


Next
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

TextBox1.Clear()

End Sub
End Class

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
Sample Code

Here's my entire code for this example. It's a bit rough but it works. This is for a console application, but you can adapt it for the windows forms.

Module Module1

    Sub Main()
        Dim intMatrix(6, 4) As Integer

        If intMatrix.GetUpperBound(1) > 0 AndAlso intMatrix.GetUpperBound(0) > 0 Then
            Dim count As Integer = 0
            Dim NumberCount As Integer = 1
            Dim col As Integer = intMatrix.GetUpperBound(1) - 1

            For row As Integer = intMatrix.GetUpperBound(0) To 0 Step -1
                If count > 0 Then
                    intMatrix(row, col) += 1
                    col -= 1
                ElseIf count = 0 Then
                    NumberCount += 1
                    count = NumberCount
                End If

                count -= 1
            Next
        End If

        For row As Integer = 0 To intMatrix.GetUpperBound(0)
            For col As Integer = 0 To intMatrix.GetUpperBound(1)
                Console.Write(intMatrix(row, col))
            Next

            Console.WriteLine()
        Next

        Console.ReadLine()
    End Sub

End Module


already wrote this so i'm just gonna paste this example here to, this one is the same thing, just uses a class instead
Module Module1

    Sub Main()
        Dim mMatrix As New Matrix(20, 20, False)

        Console.WriteLine(mMatrix.ToString())

        Console.WriteLine("Apply Pattern")

        mMatrix.ApplyPattern()

        Console.WriteLine()
        Console.WriteLine(mMatrix.ToString())

        Console.ReadLine()
    End Sub

End Module

Class Matrix
    Private intMatrix(,) As Integer

    Sub New(ByVal x As Integer, ByVal y As Integer, Optional ByVal zeroBased As Boolean = True)
        If zeroBased Then
            ReDim intMatrix(x, y)
        Else
            ReDim intMatrix(x - 1, y - 1)
        End If
    End Sub

    Function ApplyPattern() As Boolean
        If intMatrix.GetUpperBound(1) > 0 AndAlso intMatrix.GetUpperBound(0) > 0 Then
            Dim count As Integer = 0
            Dim NumberCount As Integer = 1
            Dim col As Integer = intMatrix.GetUpperBound(1)

            For row As Integer = intMatrix.GetUpperBound(0) To 0 Step -1
                If count > 0 Then
                    intMatrix(row, col) += 1
                ElseIf count = 0 Then
                    NumberCount += 1
                    count = NumberCount
                End If

                col -= 1
                count -= 1
            Next
        End If

        Return True
    End Function

    Public Overrides Function ToString() As String
        Dim strReturn As String = ""

        For row As Integer = 0 To intMatrix.GetUpperBound(0)
            For col As Integer = 0 To intMatrix.GetUpperBound(1)
                strReturn &= intMatrix(row, col)
            Next

            strReturn &= Environment.NewLine
        Next

        Return strReturn
    End Function
End Class



Here is some code using the class posted above in a windows forms app for a textbox. Note that the textbox Multiline property needs to be set to true
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim matrix As New Matrix(20, 20, False)
        matrix.ApplyPattern()

        TextBox1.Text = matrix.ToString
    End Sub
End Class
 
writing a code in matrix

one last question...

how can I make it as a numerical value?? I mean the output..Because im dealing w/ real numbers and not strings...
after constructing the matrix in the form i wanted....It will then be evaluated by using some method in MATRIX say gaussian elim or gauss-jordan... Then, it should be real values and not as strings...the output I have just requested from you will be inserted in one of my matrix output....I was able to solve the outputs an matrix except the one I requested from you........


this is my whole source code::

////////////////////////////////////////////

VB.NET:
Public Class Form1
    Dim factor As Double
    Dim x(2000) As Double, sum As Double
    Dim Cells(8000, 8000) As Double
    Dim k As Integer
    Dim pc As Double = 0
    Dim pw As Double = 0
    Dim g As Double = 0
    Dim w As Double = 0
    Dim h1 As Double = 0
    Dim h2 As Double = 0
    Dim w1 As Double = 0
    Dim w2 As Double = 0
    Dim dx As Double
    Dim a(8000, 8000) As Double, b(8000) As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MATRIX_A() ' h1=h2 rectangular shape or square
        MATRIX_B() ' h1<>h2 irregular shape
        SOLUTION() 'solution

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        TextBox7.Clear()
        TextBox8.Clear()
        TextBox9.Clear()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox10.Clear()

    End Sub



    Private Sub MATRIX_A()
        Dim i As Integer, j As Integer, n As Integer, i1 As Integer, j1 As Integer, n1 As Integer = 2
        Dim h1 As Double = 0, h2 As Double

        Try
            'Check for Integers first
            If Not Double.TryParse(TextBox1.Text, h1) Then
                Throw New ApplicationException( _
                    "The first number must be an integer")
            End If
            If Not Double.TryParse(TextBox2.Text, h2) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox3.Text, w1) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox4.Text, w2) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")

            End If
            If Not Double.TryParse(TextBox5.Text, pc) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox6.Text, pw) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox7.Text, g) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox8.Text, dx) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If

            n = h2 / dx * (w1 / dx - 1) + w2 / dx * (h1 / dx - h2 / dx - 1) + (w1 / dx - w2 / dx - 2) * 1 / 2 * (w1 / dx - w2 / dx - 1)
            h1 = h2

            If h1 = h2 Then

                For i = w1 / dx To (w1 / dx - 1) * (h1 / dx - 2) + 1 Step w1 / dx - 1
                    For j = w1 / dx To n + 1 Step n
                        a(i, n + 1) = pw * g * (1 / 12) ^ 2 * (1 / 1000) * (h1 - n1 * dx)
                        n1 = n1 + 1
                    Next j
                Next i

                a(1, n + 1) = pc * g * h1 * (1 / 12) ^ 2 * (1 / 1000) + pw * g * (h1 - dx) * (1 / 12) ^ 2 * (1 / 1000)

                For i = 2 To w1 / dx Step 1
                    For j = n + 1 To (w1 / dx - 2) * (n + 1) Step n + 1
                        a(i, n + 1) = pc * g * h1 * (1 / 12) ^ 2 * (1 / 1000)
                    Next
                Next

                i = j
                For i = 1 To n
                    For j = 1 To n

                        If i = j Then
                            a(i, j) = -4

                        ElseIf i - 1 = j Then
                            a(i, j) = 1

                            For i1 = w1 / dx To (w1 / dx - 1) * (h1 / dx - 2) + 1 Step w1 / dx - 1
                                For j1 = w1 / dx - 1 To i1 - 1 Step w1 / dx - 1
                                    a(i1, j1) = 0
                                Next
                            Next

                        ElseIf i + 1 = j Then
                            a(i, j) = 1

                            For i1 = w1 / dx - 1 To (w1 / dx - 1) * (h1 / dx - 2) Step w1 / dx - 1
                                For j1 = w1 / dx To i1 + 1 Step w1 / dx - 1
                                    a(i1, j1) = 0
                                Next
                            Next

                        ElseIf i + w1 / dx - 1 = j And w1 = w2 Then
                            a(i, j) = 1

                        ElseIf w1 <> w2 And j = i + (w1 / dx - 1) Then

                            a(i, j) = 1
                            a((w1 / dx - 1) * h2 / dx, (w1 / dx - 1) * h2 / dx + w1 / dx - 1) = 0

                            If i > (w1 / dx - 1) * h2 / dx Then

                                a(i, j) = 0

                            End If

                            ElseIf i - (w1 / dx - 1) = j And w1 = w2 Then
                                a(i, j) = 1


                        ElseIf i - (w1 / dx - 1) = j And w1 <> w2 Then

                            a(i, j) = 1

                            a((w1 / dx - 1) * h2 / dx + w1 / dx - 1, (w1 / dx - 1) * h2 / dx) = 0

                            If i > (w1 / dx - 1) * h2 / dx + w1 / dx - 1 Then

                                a(i, j) = 0

                            End If

                            Else
                                a(i, j) = 0

                            End If
                    Next j
                Next i

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error in Entry", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub MATRIX_B()
        Dim i As Integer, j As Integer, n As Integer, i1 As Integer, j1 As Integer, i2 As Integer, i3 As Integer
        Dim h3 As Integer, i4 As Integer, j4 As Integer, n1 As Integer = 2, i5 As Integer, j5 As Integer, i6 As Integer
        Dim i1a As Integer, i2a As Integer, i7 As Integer, j7 As Integer, i8 As Integer, j8 As Integer
        Dim i9 As Integer, j9 As Integer, i10 As Integer, j10 As Integer, i2b As Integer


        Try
            'Check for Integers first
            If Not Double.TryParse(TextBox1.Text, h1) Then
                Throw New ApplicationException( _
                    "The first number must be an integer")
            End If
            If Not Double.TryParse(TextBox2.Text, h2) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox3.Text, w1) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox4.Text, w2) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")

            End If
            If Not Double.TryParse(TextBox5.Text, pc) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox6.Text, pw) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox7.Text, g) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If
            If Not Double.TryParse(TextBox8.Text, dx) Then
                Throw New ApplicationException( _
                    "The second number must be an integer")
            End If

            If h1 <> h2 Then
                n = h2 / dx * (w1 / dx - 1) + w2 / dx * (h1 / dx - h2 / dx - 1) + (w1 / dx - w2 / dx - 2) * 1 / 2 * (w1 / dx - w2 / dx - 1)

                a(1, n + 1) = pc * g * h1 * (1 / 12) ^ 2 * (1 / 1000) + pw * g * (h1 - dx) * (1 / 12) ^ 2 * (1 / 1000)

                For i = 2 To w1 / dx Step 1
                    For j = n + 1 To (w1 / dx - 2) * (n + 1) Step n + 1
                        a(i, n + 1) = pc * g * h1 * (1 / 12) ^ 2 * (1 / 1000)
                    Next
                Next

                i = j
                For i = 1 To n
                    For j = 1 To n

                        If i = j Then
                            a(i, j) = -4

                        ElseIf i = j + 1 And h3 < (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1 Then
                            a(i, j) = 1

                            i3 = 1
                            Do While i3 < h1 / dx
                                i3 = i3 + 1
                                h3 = i3 * dx

                            Loop

                            For i1 = w1 / dx To (w1 / dx - 1) * h2 / dx + 1 Step w1 / dx - 1
                                For j1 = w1 / dx - 1 To (w1 / dx - 1) * h2 / dx Step w1 / dx - 1
                                    a(i1, j1) = 0
                                Next
                            Next

                            For i5 = w1 / dx To (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1 Step w1 / dx - 1
                                For j5 = w1 / dx To n + 1 Step n
                                    a(i5, n + 1) = pw * g * (1 / 12) ^ 2 * (1 / 1000) * (h1 - n1 * dx)
                                    n1 = n1 + 1
                                Next j5
                            Next i5

                            a(i, j) = 1

                            For i1 = w1 / dx - 1 To (w1 / dx - 1) * h2 / dx Step w1 / dx - 1
                                For j1 = w1 / dx To i1 + 1 Step w1 / dx - 1
                                    a(i1, j1) = 0
                                Next
                            Next

                            '///////////////h3 >= (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1////////


                            If i = j + 1 And h3 >= (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1 Then

                                a(i, j) = 1

                                i1 = (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1
                                j1 = (w1 / dx - 1) * (h2 / dx) + w1 / dx - 2

                                a(i1, j1) = 0
                                i2 = 2
                                Do While i2 <= (h1 - h2 - 2 * dx) / dx
                                    i2 = i2 + 1
                                    i1 = i1 + (w1 - i2 * dx) / dx
                                    j1 = j1 + (w1 - i2 * dx) / dx
                                    i4 = i1
                                    j4 = j1
                                    a(i4, j4) = 0
                                Loop

                                i1a = (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1

                                a(i1a, n + 1) = pw * g * (1 / 12) ^ 2 * (1 / 1000) * (h1 - n1 * dx)
                                n1 = n1 + 1
                                i2a = 2

                                Do While i2a <= (h1 - h2 - 2 * dx) / dx
                                    i2a = i2a + 1
                                    i1a = i1a + (w1 - i2a * dx) / dx
                                    i6 = i1a
                                    a(i6, n + 1) = pw * g * (1 / 12) ^ 2 * (1 / 1000) * (h1 - n1 * dx)
                                    n1 = n1 + 1

                                Loop

                                a(i, j) = 1

                                i1 = (w1 / dx - 1) * (h2 / dx) + w1 / dx - 2
                                j1 = (w1 / dx - 1) * (h2 / dx) + w1 / dx - 1

                                a(i1, j1) = 0
                                i2 = 2

                                Do While i2 <= (h1 - h2 - 2 * dx) / dx
                                    i2 = i2 + 1
                                    i1 = i1 + (w1 - i2 * dx) / dx
                                    j1 = j1 + (w1 - i2 * dx) / dx
                                    i7 = i1
                                    j7 = j1
                                    a(i7, j7) = 0
                                Loop

                                '///////////////////start here!!/////////////

                                i8 = (w1 / dx - 1) * h2 / dx + 1
                                j8 = i8 + (w1 - 2 * dx) / dx

                                a(i8, j8) = 1

                                i2 = 1


                                Do While i2 < (h1 - h2 - 2 * dx) / dx

                                    i2 = i2 + 1
                                    i8 = i8 + (w1 - i2 * dx) / dx
                                    j8 = j8 + (w1 - i2 * dx) / dx - 1
                                    i9 = i8
                                    j9 = j8

                                    a(i9, j9) = 1

                                Loop

                            End If

                            '///////////////////end here!!/////////////


                        End If
                    Next j
                Next i


            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error in Entry", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub SOLUTION()

        Dim i As Integer, j As Integer, n As Integer

        TextBox9.Text = h2 / dx * (w1 / dx - 1) + w2 / dx * (h1 / dx - h2 / dx - 1) + (w1 / dx - w2 / dx - 2) * 1 / 2 * (w1 / dx - w2 / dx - 1)
        n = TextBox9.Text
        n = h2 / dx * (w1 / dx - 1) + w2 / dx * (h1 / dx - h2 / dx - 1) + (w1 / dx - w2 / dx - 2) * 1 / 2 * (w1 / dx - w2 / dx - 1)
        i = j

        For i = 1 To n Step 1
            For j = 1 To n + 1 Step 1
                TextBox10.AppendText(a(i, j) & vbTab)
                Application.DoEvents()
            Next j
            b(i) = a(i, n + 1)
        Next i

    End Sub

End Class

////////////////////////////////////////////////

and this is the complete out put in 34 eqtn by 34 unknowns;;; and my actual problem is up to 2000 by 2000:::::

-410000010000
00000000000000000000002.22
1-4100000100000000000000000000000001.61
01-410000010000000000000000000000001.61
001-41000001000000000000000000000001.61
0001-4100000100000000000000000000001.61
00001-410000010000000000000000000001.61
000001-40000001000000000000000000001.61
1000000-4100000100000000000000000000.70
01000001-410000010000000000000000000.00
001000001-41000001000000000000000000.00
0001000001-4100000100000000000000000.00
00001000001-410000010000000000000000.00
000001000001-41000001000000000000000.00
0000001000001-4000000000000000000000.00
00000001000000-410000100000000000000.70
000000001000001-41000010000000000000.00
0000000001000001-4100001000000000000.00
00000000001000001-410000100000000000.00
000000000001000001-41000010000000000.00
0000000000001000001-4000000000000000.00
00000000000000100000-410001000000000.70
000000000000000100001-41000100000000.00
0000000000000000100001-4100010000000.00
00000000000000000100001-410001000000.00
000000000000000000100001-40000000000.00
0000000000000000000010000-4100100000.70
00000000000000000000010001-410010000.00
000000000000000000000010001-41001000.00
0000000000000000000000010001-4000000.00
00000000000000000000000001000-410100.70
000000000000000000000000001001-41010.00
0000000000000000000000000001001-4000.00
00000000000000000000000000000100-410.70
000000000000000000000000000000101-40.00
 
Last edited by a moderator:
Maybe someone else on here can help?

one last question...

how can I make it as a numerical value?? I mean the output..Because im dealing w/ real numbers and not strings...
after constructing the matrix in the form i wanted....It will then be evaluated by using some method in MATRIX say gaussian elim or gauss-jordan... Then, it should be real values and not as strings...the output I have just requested from you will be inserted in one of my matrix output....I was able to solve the outputs an matrix except the one I requested from you........

I'm sorry, I don't have the time to look over your code and i don't really know much about Matrix's.
Maybe someone else on here or one of the mods can help you with it...



As far as the output, in a textbox / console, i do not believe there is a way to keep it in numeric form. Either way it gets converted to a string, if i'm not mistaken? Since you already have the array as a numeric data type, maybe you can just work with that, then just output it to the textbox when your done?
 
Back
Top