creating a matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dr3amxstar
    New Member
    • May 2007
    • 55

    creating a matrix

    Hi.. Im new to visual basic 2005 express edition and need some help. I am currently doing a project on developing Artifical Neural Network and is kind of stuck

    First of all, i need to store values from 3 textbox into a matrix and then multiply it with another matrix ( Standard number). Can anyone tell me how do i go about doing it?

    Thanks!
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    There may be better ways, but the simplest would be to store them in an array, then use a loop to run through the array and do the multiplication. For more than one dimension, just use nested loops.

    Comment

    • dr3amxstar
      New Member
      • May 2007
      • 55

      #3
      Originally posted by Killer42
      There may be better ways, but the simplest would be to store them in an array, then use a loop to run through the array and do the multiplication. For more than one dimension, just use nested loops.
      Hihi thanks for the help! I did thought of arrays too but im dealing with matrix size of up to 40 x 273. Do you mind given me an example of how to create a array of more then one dimension using nested loops?

      Comment

      • danp129
        Recognized Expert Contributor
        • Jul 2006
        • 323

        #4
        Code:
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim myarray(0 To 39, 0 To 272) As Long
                Dim lngcounter&, x&, y& ' & means ' as long'
        
                'set values with value of counter
                For x = 0 To 39
                    For y = 0 To 272
                        myarray(x, y) = lngcounter
                        lngcounter = lngcounter + 1
                    Next 'y
                Next 'x
        
                
                'print out values in array
                For x = 0 To 39
                    For y = 0 To 272
                        Debug.Print("x" & x & "," & "y" & y & "=" & myarray(x, y))
                    Next 'y
                Next 'x
        
        
                'multiply two values
                Debug.Print(myarray(20, 20) & " * " & myarray(30, 30) & "=" & myarray(20, 20) * myarray(30, 30))
        x0,y0=0
        x0,y1=1
        x0,y2=2
        x0,y3=3
        .....
        x39,y270=10917
        x39,y271=10918
        x39,y272=10919
        5480 * 8220=45045600

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Thanks Dan.

          I'd just like to point out that the type declaration characters like "&" are basically the old way of doing it, and generally not recommended. "As Long" makes for code that's much easier to read.

          Comment

          • danp129
            Recognized Expert Contributor
            • Jul 2006
            • 323

            #6
            Originally posted by Killer42
            Thanks Dan.

            I'd just like to point out that the type declaration characters like "&" are basically the old way of doing it, and generally not recommended. "As Long" makes for code that's much easier to read.
            Yeah but I don't like typing it multiple times and I did include what it meant :)

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by danp129
              Yeah but I don't like typing it multiple times and I did include what it meant :)
              Fair enough.

              I wasn't correcting you, it was just meant as a bit of a heads-up to the OP. Which way you do it is pretty much a matter of personal preference, as long as they're both supported. I think we can safely assume that the results are exactly the same either way.

              Comment

              • dr3amxstar
                New Member
                • May 2007
                • 55

                #8
                Originally posted by danp129
                Code:
                    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                        Dim myarray(0 To 39, 0 To 272) As Long
                        Dim lngcounter&, x&, y& ' & means ' as long'
                
                        'set values with value of counter
                        For x = 0 To 39
                            For y = 0 To 272
                                myarray(x, y) = lngcounter
                                lngcounter = lngcounter + 1
                            Next 'y
                        Next 'x
                
                        
                        'print out values in array
                        For x = 0 To 39
                            For y = 0 To 272
                                Debug.Print("x" & x & "," & "y" & y & "=" & myarray(x, y))
                            Next 'y
                        Next 'x
                
                
                        'multiply two values
                        Debug.Print(myarray(20, 20) & " * " & myarray(30, 30) & "=" & myarray(20, 20) * myarray(30, 30))
                x0,y0=0
                x0,y1=1
                x0,y2=2
                x0,y3=3
                .....
                x39,y270=10917
                x39,y271=10918
                x39,y272=10919
                5480 * 8220=45045600
                Hihi Thanks alot for all the help! I used just a single dimension previously until i saw ur code and found out that its better. But may i know what is the difference between one dimension and 2 dimension?

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by dr3amxstar
                  Hihi Thanks alot for all the help! I used just a single dimension previously until i saw ur code and found out that its better. But may i know what is the difference between one dimension and 2 dimension?
                  Well, it's just like the difference between a list and a grid. In a one-dimensional array each element is identified by one index. In a two-dimensional array, each is identified by two indices. So any time you have a series of values which are dependent on two things (such as an X and Y coordinate), a two-dimensional array may be a convenient way to store them.

                  It is usual to think of, and graphically represent, a two-dimensional array as a square, with coordinates like those used in a street directory map, or on a computer screen, or a spreadsheet. A three-dimensional array is typically shown as representing a position within a cube. But in fact you can have pretty much any number of dimensions in an array - they just get harder to visualise, the more you have.

                  Comment

                  • dr3amxstar
                    New Member
                    • May 2007
                    • 55

                    #10
                    Thanks to all of the help given , i have been able to draft out the body for my artificial neural network. But the problem i encountered now is how to feed in inputs from files to my program automatically. I have 13 files each looking like this

                    HKSEVAHRFKDLGEE NFKALVLIAFAQYLQ QCPFEDHVKLVHKSE VAHRFKDLGEENFKA LVLIAFAQYLQQCP -> Input
                    LLLHHHHHHHHHHHH HHHHHHHHHHHHHHL LLLLHHHHHHHLLLH HHHHHHHHHHHHHHH HHHHHHHHHHLLLL -> desired output

                    HKSEVAHRFKDLGEE NFKALVLIAFAQYLQ QCPFEDHVKLVHKSE VAHRFKDLGEENFKA LVLIAFAQYLQQCP -> Input
                    LLLHHHHHHHHHHHH HHHHHHHHHHHHHHL LLLLHHHHHHHLLLH HHHHHHHHHHHHHHH HHHHHHHHHHLLLL -> desired output

                    (whole pages full of sets of 2 lines)

                    This are my training sets






                    The alphabets are amino acid and are to be converted to 21 bits using sparse code. I have to create a window size of 13 ( input is 13 at one time), where by the desired output of the middle amino acid will be used for calculation in the middle of the program.

                    How do i go about to let my program automatically load the file(total of 13 such files) from my computer drive, make it window slide 13 at a time and i will take the desired output of the middle of the 13 inputs( convert all the sparse coding for processing). *6 x will have to be included at the start so that the first alphabet can be at the middle. Should i store all this is a 2 dimensional array? Is this confusing?

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by dr3amxstar
                      ...Is this confusing?
                      Yes!

                      Sorry, but I got completely lost.

                      I'm having great difficulty understanding the goal. But from what I have seen, if we can get past that, I'd say the actual code will be fairly simple.

                      Comment

                      • dr3amxstar
                        New Member
                        • May 2007
                        • 55

                        #12
                        Originally posted by Killer42
                        Yes!

                        Sorry, but I got completely lost.

                        I'm having great difficulty understanding the goal. But from what I have seen, if we can get past that, I'd say the actual code will be fairly simple.
                        Haha i also dont know what i am blabbering. Maybe to simplify :
                        1) how do i read files from my directory on my computer to the program?

                        2) Does the program reads line by line? How do i make it read the second line or third automatically after the first line is completed

                        3) Is there a code that actually enable the program to open and read another new file after it is done with the current file.

                        3) is there a function that does sliding from one word to another? because at any one time i only want 13 alphabets from that long string and after one loops it moves to the next alphabet like this :



                        For every 13 alphabet in the first row ( first line)(store all 13 in array), i also want the number 7 alphabet out of the 13 alphabet in the second row (second line). thats why i have to process one by one because in one loop there can be only one desired output(alphabet s in second row) and 13 inputs( alphabets in first row)
                        Thanks a thousand!

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by dr3amxstar
                          Haha i also dont know what i am blabbering. Maybe to simplify :
                          1) how do i read files from my directory on my computer to the program?

                          2) Does the program reads line by line? How do i make it read the second line or third automatically after the first line is completed

                          3) Is there a code that actually enable the program to open and read another new file after it is done with the current file.

                          3) is there a function that does sliding from one word to another? because at any one time i only want 13 alphabets from that long string and after one loops it moves to the next alphabet like this :

                          <img>

                          For every 13 alphabet in the first row ( first line)(store all 13 in array), i also want the number 7 alphabet out of the 13 alphabet in the second row (second line). thats why i have to process one by one because in one loop there can be only one desired output(alphabet s in second row) and 13 inputs( alphabets in first row)
                          Thanks a thousand!
                          Hm... well, I feel I'm gradually progressing toward an understanding. Certainly the file handling sounds simple enough.

                          Actually, this is good timing. I've just recently put a couple of samples in the Articles section that show how to read a text file. One reads line by line, the other reads it in one go, into one big string. Check out the index at the top of the section. Perhaps you can give me some feedback on whether the code samples are actually any use. It's hard for me to judge, since I wrote them.

                          Also, as far as looping through the files, there are a couple of techniques you can use.

                          There's the built-in Dir() function. When you pass it a string, it will return the name of the first file that matches. Pass it nothing, it will look for the next one. For example, this code displays the names of all files in the C: drive root directory (though hopefully there wouldn't be very many.

                          [CODE=vb]Dim FileName As String
                          FileName = Dir("C:\*.*")
                          Do While FileName <> ""
                          Debug.Print FileName
                          FileName = Dir
                          Loop[/CODE]

                          Then there is FilesystemObjec t. To use this, you must add a reference to Microsoft Scripting Runtime to your project. And it's a little more complex to work with. But it offers a lot of useful functionality when working with drives, folders and files.

                          As for processing one file after another, the above Do While loop could just as easily be calling a routine to do something with the files, rather than just printing their names to the debugging window. (One thing to keep in mind with this sort of technique is that Dir returns only the filename, no path.)

                          I'm still having some trouble with the "alphabet" thing. For starters, what is it that you are referring to as "an alphabet"? When you say, for instance, "For every 13 alphabet in the first row" do you mean for every 13 characters?

                          Comment

                          • dr3amxstar
                            New Member
                            • May 2007
                            • 55

                            #14
                            Originally posted by Killer42
                            I'm still having some trouble with the "alphabet" thing. For starters, what is it that you are referring to as "an alphabet"? When you say, for instance, "For every 13 alphabet in the first row" do you mean for every 13 characters?
                            Haha all this seems like stranger to me but i will go and learn! Really thanks for your advice and thanks for guiding me to the right path!

                            Haha i think "alphabets" is the same as characters? Because in my notepad it does not contain normal words or sentences. It is filled with this :

                            HKSEVAHRFKDLGRR N
                            LLLHHHHHHHLLLHH HH

                            This is not in any wierd language but just that each alphabets represent an amino acid. I will have to program it such that it takes in 13 "alphabets" or characters at one time, then during the second loop it will shift one alphabets to the right and take in another 13 characters. haha do i make sense?

                            Comment

                            • dr3amxstar
                              New Member
                              • May 2007
                              • 55

                              #15
                              Hi i went to the code you posted. I am quite slow at understanding so need some help there. Correct me if im wrong, does it LineNo = LineNo + 1 means that it automatically gets the next line instead of keep typing a lot of these :
                              StrVar = readerVar.ReadL ine
                              StrVar = readerVar.ReadL ine

                              What is FileNo = FreeFile?

                              Where is the part in the code that allows me to put in the directory or file name to be included in my program. Hope what i say make sense!

                              Comment

                              Working...