473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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!
May 11 '07 #1
50 5611
Killer42
8,435 Expert 8TB
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.
May 11 '07 #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.
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?
May 11 '07 #3
danp129
323 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim myarray(0 To 39, 0 To 272) As Long
  3.         Dim lngcounter&, x&, y& ' & means ' as long'
  4.  
  5.         'set values with value of counter
  6.         For x = 0 To 39
  7.             For y = 0 To 272
  8.                 myarray(x, y) = lngcounter
  9.                 lngcounter = lngcounter + 1
  10.             Next 'y
  11.         Next 'x
  12.  
  13.  
  14.         'print out values in array
  15.         For x = 0 To 39
  16.             For y = 0 To 272
  17.                 Debug.Print("x" & x & "," & "y" & y & "=" & myarray(x, y))
  18.             Next 'y
  19.         Next 'x
  20.  
  21.  
  22.         'multiply two values
  23.         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
May 12 '07 #4
Killer42
8,435 Expert 8TB
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.
May 12 '07 #5
danp129
323 Expert 256MB
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 :)
May 12 '07 #6
Killer42
8,435 Expert 8TB
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.
May 12 '07 #7
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim myarray(0 To 39, 0 To 272) As Long
  3.         Dim lngcounter&, x&, y& ' & means ' as long'
  4.  
  5.         'set values with value of counter
  6.         For x = 0 To 39
  7.             For y = 0 To 272
  8.                 myarray(x, y) = lngcounter
  9.                 lngcounter = lngcounter + 1
  10.             Next 'y
  11.         Next 'x
  12.  
  13.  
  14.         'print out values in array
  15.         For x = 0 To 39
  16.             For y = 0 To 272
  17.                 Debug.Print("x" & x & "," & "y" & y & "=" & myarray(x, y))
  18.             Next 'y
  19.         Next 'x
  20.  
  21.  
  22.         'multiply two values
  23.         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?
May 21 '07 #8
Killer42
8,435 Expert 8TB
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.
May 21 '07 #9
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

HKSEVAHRFKDLGEENFKALVLIAFAQYLQQCPFEDHVKLVHKSEVAHRF KDLGEENFKALVLIAFAQYLQQCP -> Input
LLLHHHHHHHHHHHHHHHHHHHHHHHHHHLLLLLHHHHHHHLLLHHHHHH HHHHHHHHHHHHHHHHHHHHLLLL -> desired output

HKSEVAHRFKDLGEENFKALVLIAFAQYLQQCPFEDHVKLVHKSEVAHRF KDLGEENFKALVLIAFAQYLQQCP -> Input
LLLHHHHHHHHHHHHHHHHHHHHHHHHHHLLLLLHHHHHHHLLLHHHHHH HHHHHHHHHHHHHHHHHHHHLLLL -> 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?
May 22 '07 #10
Killer42
8,435 Expert 8TB
...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.
May 22 '07 #11
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(alphabets in second row) and 13 inputs( alphabets in first row)
Thanks a thousand!
May 22 '07 #12
Killer42
8,435 Expert 8TB
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(alphabets 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.

Expand|Select|Wrap|Line Numbers
  1. Dim FileName As String
  2. FileName = Dir("C:\*.*")
  3. Do While FileName <> ""
  4.   Debug.Print FileName
  5.   FileName = Dir
  6. Loop
Then there is FilesystemObject. 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?
May 22 '07 #13
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 :

HKSEVAHRFKDLGRRN
LLLHHHHHHHLLLHHHH

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?
May 23 '07 #14
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.ReadLine
StrVar = readerVar.ReadLine

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!
May 23 '07 #15
Killer42
8,435 Expert 8TB
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 :

HKSEVAHRFKDLGRRN
LLLHHHHHHHLLLHHHH

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?
I guess that's reasonably straightforward. You want to deal with the first 13 bytes (might be easier if we call them that) from the record. Then the 2nd to 14th bytes. Then the 3rd to 15th bytes, and so on. Correct?
May 23 '07 #16
I guess that's reasonably straightforward. You want to deal with the first 13 bytes (might be easier if we call them that) from the record. Then the 2nd to 14th bytes. Then the 3rd to 15th bytes, and so on. Correct?
yes yes thats it! and after that line ended the program will automatically move to the next line.

I was also thinking of using streamreader so that i am able to store each byte in array. is there other alternative as well?
May 23 '07 #17
Killer42
8,435 Expert 8TB
yes yes thats it! and after that line ended the program will automatically move to the next line.

I was also thinking of using streamreader so that i am able to store each byte in array. is there other alternative as well?
I tend to just use the built-in statements such as Open and Line Input# to read a text file line-by-line. However, that's because I've never learned how to work with things like streamreader. So I can't really advise you which way to go.

One important concept to keep in mind is to "encapsulate" or "modularise" your code where possible. In other words, keep the parts of your code fairly independent of each other, so they can be easily replaced.

For example, if you set up your code to read a file line by line, ideally it should pass each line to a separate routine which does the work with them. The inner workings shouldn't be too tightly intertwined with the file-reading code. That way, you can easily change your mind about how to read the file, or how to do the processing of the lines, without affecting the rest of the code.

Unfortunately I don't have the time right now, or I'd have a look at how streamreaders work.

In any case, as long as it provides what you want, which way you go is really a matter of personal preference.

P.S. Definition - alphabet: a character set that includes letters and is used to write a language
May 23 '07 #18
I tend to just use the built-in statements such as Open and Line Input# to read a text file line-by-line. However, that's because I've never learned how to work with things like streamreader. So I can't really advise you which way to go.

One important concept to keep in mind is to "encapsulate" or "modularise" your code where possible. In other words, keep the parts of your code fairly independent of each other, so they can be easily replaced.

For example, if you set up your code to read a file line by line, ideally it should pass each line to a separate routine which does the work with them. The inner workings shouldn't be too tightly intertwined with the file-reading code. That way, you can easily change your mind about how to read the file, or how to do the processing of the lines, without affecting the rest of the code.

Unfortunately I don't have the time right now, or I'd have a look at how streamreaders work.

In any case, as long as it provides what you want, which way you go is really a matter of personal preference.

P.S. Definition - alphabet: a character set that includes letters and is used to write a language
I am really glad enough that you actually took some time off ur busy schedule to guide me along!

Points taken! But if i does it your way, when i read the line by line, is it possible to assign each bytes to array?

EFTGGRABC
assign E to array(0)
assign F to array(1)
assign T to array(2)
and so on till the 13th bytes
May 23 '07 #19
Killer42
8,435 Expert 8TB
I am really glad enough that you actually took some time off ur busy schedule to guide me along!

Points taken! But if i does it your way, when i read the line by line, is it possible to assign each bytes to array?

EFTGGRABC
assign E to array(0)
assign F to array(1)
assign T to array(2)
and so on till the 13th bytes
Sure!

While there are certainly more sophisticated (and efficient) ways to do it, one very simple method is just to loop through, using Mid() function to retrieve each character from the string, and place it in the array. Quick sample...

Expand|Select|Wrap|Line Numbers
  1. Dim S As String, L As Long
  2. Dim Char(1 To 16) As String * 1   ' Or perhaps As Byte
  3. S = "This is a string"
  4. For L = 1 To 16
  5.   Char(L) = Mid$(S, L, 1)
  6. Next
May 24 '07 #20
Sure!

While there are certainly more sophisticated (and efficient) ways to do it, one very simple method is just to loop through, using Mid() function to retrieve each character from the string, and place it in the array. Quick sample...

Expand|Select|Wrap|Line Numbers
  1. Dim S As String, L As Long
  2. Dim Char(1 To 16) As String * 1   ' Or perhaps As Byte
  3. S = "This is a string"
  4. For L = 1 To 16
  5.   Char(L) = Mid$(S, L, 1)
  6. Next
Thanks! I think it should work but there is some errors:

Dim Char(1 To 16) As String* 1
When i debug, it says that Array lower bound can only be zero
So i change it to zero but this code
For L = 1 To 16
Char(L)=Mid$(S,L,1)
When i change to L = 0 To 16
It stated Start must be bigger then zero
May 24 '07 #21
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim source As String = "XXXXXXAEFGHIW"
  5.         Dim characters As String = _
  6.         "ACDEFGHIKLMNPQRSTVYWX"""
  7.         Dim sparse() As String = { _
  8.         "100000000000000000000", _
  9.         "010000000000000000000", _
  10.         "001000000000000000000", _
  11.         "000100000000000000000", _
  12.         "000010000000000000000", _
  13.         "000001000000000000000", _
  14.         "000000100000000000000", _
  15.         "000000010000000000000", _
  16.         "000000001000000000000", _
  17.         "000000000100000000000", _
  18.         "000000000010000000000", _
  19.         "000000000001000000000", _
  20.         "000000000000100000000", _
  21.         "000000000000010000000", _
  22.         "000000000000001000000", _
  23.         "000000000000000100000", _
  24.         "000000000000000010000", _
  25.         "000000000000000001000", _
  26.         "000000000000000000100", _
  27.         "000000000000000000010", _
  28.         "000000000000000000000"}
  29.  
  30.         Dim result As New System.Text.StringBuilder
  31.         Dim counter As Integer
  32.         Dim position As Integer
  33.  
  34.         For counter = 0 To 12
  35.             position = characters.IndexOf(Char.ToUpper( _
  36.             source.Chars(counter)))
  37.  
  38.             result.AppendLine(sparse(position))
  39.  
  40.         Next counter
  41.  
  42.         Dim Line As String = (result.ToString())
  43.  
  44.         Dim S As String, L As Long
  45.         Dim Numbers(0 To 272) As String
  46.         S = Line
  47.         For L = 1 To 272
  48.             Numbers(L) = Mid$(S, L, 1)
  49.         Next
  50.  
  51.     End Sub
  52. End Class 

In this code, my purpose is to convert the bytes in "source" to their respective sparse codes accordingly to their characters.
After converting, i want to store each "binary" in the same array but i am not able to do so. Am i on the right track?
May 24 '07 #22
OkAy this part works! Now i am going to move on to the automatic loading of files after the program finish reading one file :D
May 24 '07 #23
Expand|Select|Wrap|Line Numbers
  1. Dim FileName As String
  2. FileName = Dir("C:\*.*")
  3. Do While FileName <> ""
  4.   Debug.Print FileName
  5.   FileName = Dir
  6. Loop 
Hmm .. i would like to ask.. what does the line FileName = Dir Does?
May 24 '07 #24
Killer42
8,435 Expert 8TB
...Hmm .. i would like to ask.. what does the line FileName = Dir Does?
If you call the Dir function with no arguments, it just keeps returning the next filename matching what you previously asked for. So when I ask for "C:\*.*" it will return the first file in the C:\ directory. Then each subsequent call to Dir with no arguments will return another file, until there are no more. When that happens, the condition While FileName <> "" causes us to drop out of the loop.

Keep in mind this is all VB6 code, and may not entirely match your version - I apologise for that, but it's the best I can do at the moment. Generally the concepts will be OK (I hope), but the details of syntax may vary.
May 24 '07 #25
Killer42
8,435 Expert 8TB
OkAy this part works! Now i am going to move on to the automatic loading of files after the program finish reading one file :D
I'm glad to hear that (yes, I'm reading the thread in reverse order - sorry about that).

I couldn't follow your code, as it's in "Dotnetese". Or to put it another way, it's too advanced for me. :)

Guess it's about time I started updating to a later version.
May 24 '07 #26
If you call the Dir function with no arguments, it just keeps returning the next filename matching what you previously asked for. So when I ask for "C:\*.*" it will return the first file in the C:\ directory. Then each subsequent call to Dir with no arguments will return another file, until there are no more. When that happens, the condition While FileName <> "" causes us to drop out of the loop.

Keep in mind this is all VB6 code, and may not entirely match your version - I apologise for that, but it's the best I can do at the moment. Generally the concepts will be OK (I hope), but the details of syntax may vary.

Hmm does it mean with this code i am able to call out the file one at a time? Am i able to deal with one file first, and after i am done with this file (pass to sub routine and back)then proceed to another file whereby the program will do automatically?

Its quite a silly question but am i able to store the filename in array?
May 24 '07 #27
Killer42
8,435 Expert 8TB
Hmm does it mean with this code i am able to call out the file one at a time? Am i able to deal with one file first, and after i am done with this file (pass to sub routine and back)then proceed to another file whereby the program will do automatically?
Yes.

The sample loop that I coded could be read like this...

Expand|Select|Wrap|Line Numbers
  1. Get the first filename matching "C:\*.*"
  2. As long as we haven't run out of file names...
  3.   Print it
  4.   Get the next matching file name
  5. End of Loop
Its quite a silly question but am i able to store the filename in array?
Sure. It's a string - you can do whatever you like with it.

In cases like these, the best way to go is to try it and see what happens. Play around with it, to get a feel for what you can do.
May 24 '07 #28
Yes.

The sample loop that I coded could be read like this...

Expand|Select|Wrap|Line Numbers
  1. Get the first filename matching "C:\*.*"
  2. As long as we haven't run out of file names...
  3.   Print it
  4.   Get the next matching file name
  5. End of Loop
Sure. It's a string - you can do whatever you like with it.

In cases like these, the best way to go is to try it and see what happens. Play around with it, to get a feel for what you can do.
Hi im sorry to keep bothering you! I used your code and it works ! Thanks a thousand. I have stored it in a array but the problem now is that i am unable to load it after i pass it to another line. Am i doing it the right way?

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim FileName As String
  5.         Dim x As Integer = 0
  6.         Dim File(12) As String
  7.         FileName = Dir("D:\project1\ANN\*Chain*")
  8.         Do While FileName <> ""
  9.  
  10.             File(x) = FileName
  11.             x += 1
  12.             FileName = Dir()
  13.         Loop
  14.  
  15.  
  16.         Dim readerVar As IO.StreamReader
  17.         Dim StrVar As String
  18.         Dim n As Integer
  19.  
  20.  
  21.  
  22.         readerVar = IO.File.OpenText("D:\project1\ANN\File(1)")
  23.  
  24.         StrVar = readerVar.ReadLine
May 25 '07 #29
Killer42
8,435 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. readerVar = IO.File.OpenText("D:\project1\ANN\File(1)")
should be...
Expand|Select|Wrap|Line Numbers
  1. readerVar = IO.File.OpenText("D:\project1\ANN\" & File(1))
The colour-coding in the VB code here shows why. In your code, the "File(1)" is simply part of the string, so you are trying to open a file which is actually called "File(1)". Chances are, this is not the correct name.
May 25 '07 #30
Expand|Select|Wrap|Line Numbers
  1. readerVar = IO.File.OpenText("D:\project1\ANN\File(1)")
should be...
Expand|Select|Wrap|Line Numbers
  1. readerVar = IO.File.OpenText("D:\project1\ANN\" & File(1))
The colour-coding in the VB code here shows why. In your code, the "File(1)" is simply part of the string, so you are trying to open a file which is actually called "File(1)". Chances are, this is not the correct name.
I tried and it works .. Thanks A Lot!! Now there is progression in my program :D Thanks for your time and guidance!!
May 27 '07 #31
Helmer
10
hey may of already been suggested but u may want to look into a 3d array,
dim array(x, y, z) as long 'where x and y= dimensions of your matrix, and z= how many matrixes you have, that way you can just multiply the planes of the 3d array nice and efficient, and only 1 array is used
May 28 '07 #32
hey may of already been suggested but u may want to look into a 3d array,
dim array(x, y, z) as long 'where x and y= dimensions of your matrix, and z= how many matrixes you have, that way you can just multiply the planes of the 3d array nice and efficient, and only 1 array is used
Thanks for that advice i will try it out! One last question!

Expand|Select|Wrap|Line Numbers
  1.  Dim FileName As String
  2.  
  3.         Dim File(12) As String
  4.         FileName = Dir("D:\project1\ANN\*Chain*")
  5.         Do While FileName <> ""
  6.  
  7.             File(x) = FileName
  8.             x += 1
  9.             FileName = Dir()
  10.  
  11.         Loop
  12.  
  13.  
  14.  
  15.         Dim readerVar As IO.StreamReader
  16.         Dim source1, source2, Line1, Line2 As String
  17.         Dim n, i As Integer
  18.         Dim matrixA(0 To 273) As String
  19.         Dim Desired(0 To 3) As String
  20.         i = 0
  21.  
  22.  
  23. For i = 0 To 12
  24.  
  25.             readerVar = IO.File.OpenText("D:\project1\ANN\" & File(12))
  26.  
  27.             Do While (readerVar.Peek <> -1)
  28.  
  29.                 source1 = readerVar.ReadLine
  30.                 source2 = readerVar.ReadLine
  31.                 source1 = ("XXXXXX" & source1 & "XXXXXX")
  32.                 For n = 0 To (source1.Length - 13)
  33.  
  34.                     Line1 = source1.Substring(n, 13)
  35.                     Line2 = source2.Substring(n, 1)
  36.                     Sparsecode(Line1, matrixA)
  37.                     SSE(Line2, Desired)
  38.  
  39.  
  40.  
  41.                 Next
  42.  
  43.             Loop
  44.  
  45.         Next


[Do While (readerVar.Peek <> -1)] --> Doesnt this line suppose to terminate the loop when it comes to the end of the file and have no more line to read? In my program it just went on reading the lines over and over again when i wanted it to open another new file when it has finish reading all the line in one file.
May 29 '07 #33
SammyB
807 Expert 512MB
I'm going to let Killer answer your question because he is more familar with your code.

I just wanted to let you know that there is a Matrix Algebra package at http://www.codeproject.com/cs/librar...operations.asp. I just downloaded it and looked at the code, if you have two matrix objects A and B, then he has overloaded the * so that you can just code
Matrix C = A * B
This should help you when you get the I/O fixed up. --Sam
May 29 '07 #34
Killer42
8,435 Expert 8TB
Thanks for the link, Sammy.

Unfortunately, I've never used IO.File.OpenText in my life. I work in VB, and unless I have a specific need to play with the FileSystemObject, I just use the built-in Open statement, then Line Input to grab each line, until EOF(filenum) = True.

I guess you need to double-check the doco on the Peek property. Don't overlook the possible significance of reading two lines between Peeks. A couple of things to keep in mind...
  • Perhaps the return value changes if you keep on reading, after hitting the end of the file? (This is why the two reads may be significant).
  • Is it possible that the last line has no delimiter and this is confusing the ReadLine method?
May 29 '07 #35
I guess you need to double-check the doco on the Peek property. Don't overlook the possible significance of reading two lines between Peeks. A couple of things to keep in mind...
  • Perhaps the return value changes if you keep on reading, after hitting the end of the file? (This is why the two reads may be significant).
  • Is it possible that the last line has no delimiter and this is confusing the ReadLine method?
Well, i read on on peek property and nothing was wrong about it. through trial and error, i actually removed this line For i = 0 To 12 and the it works. It stop repeating. But then came another problem. This line is suppose to enable my program to open the next file automatically once it is done with the previous file. Did i write my code wrongly?
May 30 '07 #36
.I just wanted to let you know that there is a Matrix Algebra package at http://www.codeproject.com/cs/librar...operations.asp. I just downloaded it and looked at the code, if you have two matrix objects A and B, then he has overloaded the * so that you can just code
Matrix C = A * B
This should help you when you get the I/O fixed up. --Sam
Thanks a thousand! Once im able to solve this part i will be able to move on to the body of my code. i actually started from the body and have used loops for matrix multiplication and it look quite wierd. I guess i will look into that. :D
May 30 '07 #37
SammyB
807 Expert 512MB
I cleaned up your code. I have not figured out you input files, so this is not tested. See if this works better.
Expand|Select|Wrap|Line Numbers
  1. ' Get List of files 
  2. Dim FileName As String
  3. Dim Files As List(Of String)
  4. FileName = Dir("D:\project1\ANN\*Chain*")
  5. Do While FileName <> ""
  6.     Files.Add(FileName)
  7.     FileName = Dir()
  8. Loop
  9. ' Read files
  10. Dim readerVar As IO.StreamReader
  11. Dim source1, source2, Line1, Line2 As String
  12. Dim n, i As Integer
  13. Dim matrixA(0 To 273) As String
  14. Dim Desired(0 To 3) As String
  15. For Each FileName In Files
  16.     readerVar = IO.File.OpenText("D:\project1\ANN\" & FileName)
  17.     Do While (readerVar.Peek > -1)
  18.         source1 = readerVar.ReadLine
  19.         source2 = readerVar.ReadLine
  20.         source1 = ("XXXXXX" & source1 & "XXXXXX")
  21.         For n = 0 To (source1.Length - 13)
  22.             Line1 = source1.Substring(n, 13)
  23.             Line2 = source2.Substring(n, 1)
  24.             Sparsecode(Line1, matrixA)
  25.             SSE(Line2, Desired)
  26.         Next n
  27.     Loop
  28. Next FileName
  29.  
May 30 '07 #38
I cleaned up your code. I have not figured out you input files, so this is not tested. See if this works better.
Expand|Select|Wrap|Line Numbers
  1. ' Get List of files 
  2.  
  3. FileName = Dir("D:\project1\ANN\*Chain*")
  4. Do While FileName <> ""
  5.     Files.Add(FileName)
  6.     FileName = Dir()
  7. Loop
I tried compiling but it stated Variable 'Files' is used before it has been assigned a value. A null reference exception could result at runtime.

I have 13 input files in my directory D:\project1 which i would like the program to read them automatically once the program is done with the previous file. I tried using arrays but it didnt work..
May 31 '07 #39
SammyB
807 Expert 512MB
You forgot my line 3:

Dim Files As List(Of String)
May 31 '07 #40
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         ' Get List of files 
  3.         Dim FileName As String
  4.         Dim Files As List(Of String)
  5.         FileName = Dir("D:\project1\ANN\*Chain*")
  6.         Do While FileName <> ""
  7.             Files.Add(FileName)
  8.             FileName = Dir()
  9.         Loop
  10.         ' Read files
  11.         Dim readerVar As IO.StreamReader
  12.         Dim source1, source2, Line1, Line2 As String
  13.         Dim n As Integer
  14.         Dim matrixA(0 To 273) As String
  15.         Dim Desired(0 To 3) As String
  16.         For Each FileName In Files
  17.             readerVar = IO.File.OpenText("D:\project1\ANN\" & FileName)
  18.             Do While (readerVar.Peek > -1)
  19.                 source1 = readerVar.ReadLine
  20.                 source2 = readerVar.ReadLine
  21.                 source1 = ("XXXXXX" & source1 & "XXXXXX")
  22.                 For n = 0 To (source1.Length - 13)
  23.                     Line1 = source1.Substring(n, 13)
  24.                     Line2 = source2.Substring(n, 1)
  25.                     Sparsecode(Line1, matrixA)
  26.                     SSE(Line2, Desired)
  27.                 Next n
  28.             Loop
  29.         Next FileName
yup i did include all in but it told me that error
May 31 '07 #41
SammyB
807 Expert 512MB
Sorry, my bad. Line 4 should be

Dim Files As New List(Of String)
May 31 '07 #42
After i change that line ,the program is able to compile but not able to run. It came up with this error at this line

Expand|Select|Wrap|Line Numbers
  1. Line2 = source2.Substring(n, 1)
error: object reference not set to an instance of an object.

What does it mean?
May 31 '07 #43
SammyB
807 Expert 512MB
After i change that line ,the program is able to compile but not able to run. It came up with this error at this line

Expand|Select|Wrap|Line Numbers
  1. Line2 = source2.Substring(n, 1)
error: object reference not set to an instance of an object.

What does it mean?
Probably the second line is empty. When it fails, look at source2 and see what it contains. Also look at the FileName and then that file.
May 31 '07 #44
All my files have at least 2 lines. I added this line ListBox1.Items.Add(FileName) to check whether it is able to print all my file names but it resulted in this error

Expand|Select|Wrap|Line Numbers
  1.         FileName = Dir("F:\Documents\ANN\*Chain*")
  2.         Do While FileName <> ""
  3.  
  4.             Files.Add(FileName)
  5.             FileName = Dir()
  6.             ListBox1.Items.Add(FileName)
  7.         Loop
Error : AruguementNull Exception was unhandled.
value cannot be null
parameter name: item
May 31 '07 #45
SammyB
807 Expert 512MB
You need to switch lines 5 and 6; on the last iteration FileName will be null.
May 31 '07 #46
You need to switch lines 5 and 6; on the last iteration FileName will be null.
Sorry for keep asking! I swop and its back to this problem

Line2 = source2.Substring(n, 1)


error: object reference not set to an instance of an object.
May 31 '07 #47
SammyB
807 Expert 512MB
Sorry for keep asking! I swop and its back to this problem

Line2 = source2.Substring(n, 1)


error: object reference not set to an instance of an object.
  1. Check the file
  2. What is in source2
  3. What is len(source2)
  4. What is n
May 31 '07 #48
Expand|Select|Wrap|Line Numbers
  1. Dim FileName As String
  2.         Dim Files As New List(Of String)
  3.         FileName = Dir("D:\project1\ANN\*Chain*")
  4.         Do While FileName <> ""
  5.  
  6.             Files.Add(FileName)
  7.             ListBox1.Items.Add(Files)
  8.  
  9.             FileName = Dir()
  10.  
  11.         Loop
I run this code and got this as output
[Collection] - 13 of this

Is this the output i should get from the few lines?
Jun 1 '07 #49
oops.. I forgot to say a very big thank you for all the help given!!! Special Thanks to Killer42 and SammyB ! :D
Jun 6 '07 #50

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ivan Voras | last post by:
Is there a nice(r) way of creating a list of uniform values? I'm currently using: list('0'*N), which makes a string and then chops it up into a list. I need it to create a NxN matrix: matrix = ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown below and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you...
17
by: Bushido Hacks | last post by:
I've come up with a good set of Matrix codes recently when I cam to a road block. I would like to know if I should destroy an object to create a new object with new dimmensions. class Matrix{...
3
by: Darleen | last post by:
I am seeking conceptual here on how to get started with a "3D Matrix" in Access. We run a training center which holds multiple classes in multiple cities at multiple times. So I need to create a...
2
by: Jan Callewaert | last post by:
When compiling a library with the intel compiler icpc, I get the following warning: ld: warning: creating a DT_TEXTREL in object This happens with icc-9.0.030 and ld-2.16.1. However, this...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
4
by: crescent_au | last post by:
Hi all, I'm doing some research online on creating php-based multi-level marketing (MLM) system. It seems like a complicated system to build as I need to create one from scratch. I'd like to...
2
by: John | last post by:
Hi there, I need to create a rectangle between two points so that I can check what is inside it using Contains(). The problem I am having is how to make the rectangle be able to cope with the...
7
by: victory2006 | last post by:
i need help to make a board such as this: -----------Col 0--Col 1--Col 2 Row 0------1------1--------0 Row 1------0------0--------1 Row 2------1------1--------1 (Please ignore the dashes,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.