Array Math [RESOLVED]

jkur

Member
Joined
May 31, 2005
Messages
7
Location
Traverse City, MI
Programming Experience
1-3
I have been searching the VB help files, etc. for info on performing math functions on multidimensional arrays. I have an electronic component (A/D convertor ) that is outputing a two dimensional array of voltage values from a couple of pressure tranducers, one column for each channel (input) to the convertor. I'd like to scale the voltages to engineering units (pressure) by multiplying each element in each column and then writing the array to a text file that would have two columns of the new numbers (each column representing a channel of the a/d with scaled numbers).

Can anyone point me in the right direction to a sample or walkthrough or get me started?

thanks!

jkur (array newbie)
 
Last edited:
Sorry about the confusion.
I'd like to multiply each element in a column by a constant, and then multiply each element in the second column by a different constant. For example, column 0 multiply by 2 and column 1 multiply by 10.
 
An array is just a convenient way of grouping variables with some added functionality built in. Whenever you want to do something to every element of an arry you need to use a loop and use the loop counter as an index into the array:
VB.NET:
Const col1Factor As Integer
Const col2Factor As Integer

For i As Integer = 0 To myArray.GetUpperBound(0) Step 1
	myArray(i, 0) *= col1Factor
	myArray(i, 1) *= col2Factor
Next i
 
Or
VB.NET:
[size=2][color=#0000ff]Const[/color][/size][size=2] myNum1 [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 2
 
[/size][size=2][color=#0000ff]Const[/color][/size][size=2] myNum2 [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 10
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myArray(,) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = {{7, 4}, {5, 8}}
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sMessage [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] myArray.GetUpperBound(0)
 
myArray(i, 0) *= myNum1
 
myArray(i, 1) *= myNum2
 
sMessage &= "Result1 " & i + 1 & ": " & myArray(i, 1) & ControlChars.NewLine _
 
& "Result2 " & i + 1 & ":" & myArray(i, 0) & ControlChars.NewLine
 
[/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2]MessageBox.Show(sMessage, "MultiDimmensional array")

Cheers ;)
[/size]
 
Actually i just noted that you have an additional question about how to save result in text file ... Well, i think it is easier if you first fill a listview with given results and then just export the items to the text file ... althrough you can export/save it into text file directly.
Draw a ListView on the form and simply join this code to the button or place within load event


VB.NET:
 Dim lvitem As ListViewItem 
 
Const myNum1 As Integer = 2
 
Const myNum2 As Integer = 10
 
Dim myArray(,) As Integer = {{1, 2}, {3, 4}}
 
Dim sMessage As String
 
Dim i As Integer
 
For i = 0 To myArray.GetUpperBound(0)
 
myArray(i, 0) *= myNum1
 
sMessage &= "Column " & i + 1 & ": " & myArray(i, 0) & ControlChars.NewLine
 
lvitem = ListView1.Items.Add(myArray(i, 0))
 
myArray(i, 1) *= myNum2
 
lvitem.SubItems.Add(myArray(i, 1))
 
sMessage &= "Column " & i + 1 & ": " & myArray(i, 1) & ControlChars.NewLine
 
Next
 
MessageBox.Show(sMessage, "TITLE") 'this is optional (for demonstration only) and you should remove it if you go for listview


Cheers ;)
 
Thanks again for the help! I've had a chance to look the code over and try some things on my own ( I changed the "*" operator to "+" to do addition instead of multiplication to see if I understood where the mathematical operation was located and also added another column to the array). I will try to add this to my own code. If I have any other questions I'll post another thread.

Best regards
jkur
 
Back
Top