1 Attachment(s)
[RESOLVED] [HELP] Read from array.
Well i have attached a .cls file.. This performs linear regression operation..
I have an array with me MyArray(,)..
Now this is a 2 D array..
Wat i need to do is i need to read from the array and have to Calculate:
log(0.931227737/MyArray(i,j)) and store this in a new array.. MyArrayX()
Similarly i got to do for each of the value in the array..
And i got to read the MyArray(,) and calculate:
log(MyArray(i,j)) and store this in new arra.. MyArrayY()..
Now for the linear regression, we need to pass the X and Y value when we call the class..
The following is the code i got from the net.. Here the class is called when we click on the form..
Instead i need to call the class when we pass each of the values in the arrays..
That is when we call the class, the x value would be MyArrayX(0,0) and the corresponding y value that is passed would be MyArrayY(0,0).. Similarly MyArrayX(1,0) etc..
VB Code:
Option Explicit
Private Type PointType
X As Double
Y As Double
End Type
Dim Reg As New RegressionObject
Const R = 100
Dim P(1 To 200) As PointType
Private Sub Form_Load()
Reg.Degree = 3
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Reg
.XYAdd X, Y
P(.XYCount).X = X
P(.XYCount).Y = Y
Redraw
End With
End Sub
Private Sub Redraw()
Dim i&
Cls
With Reg
For i = 1 To .XYCount
Circle (P(i).X, P(i).Y), R
Next i
CurrentX = ScaleLeft
CurrentY = .RegVal(CDbl(ScaleLeft))
For i = ScaleLeft To ScaleWidth Step R
Line -(i, .RegVal(CDbl(i))), vbBlue \ 2
Next i
End With
End Sub
Option Explicit
Private Type PointType
X As Double
Y As Double
End Type
Dim Reg As New RegressionObject
Const R = 100
Dim P(1 To 200) As PointType
Private Sub Form_Load()
Reg.Degree = 3
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Reg
.XYAdd X, Y
P(.XYCount).X = X
P(.XYCount).Y = Y
Redraw
End With
End Sub
Private Sub Redraw()
Dim i&
Cls
With Reg
For i = 1 To .XYCount
Circle (P(i).X, P(i).Y), R
Next i
CurrentX = ScaleLeft
CurrentY = .RegVal(CDbl(ScaleLeft))
For i = ScaleLeft To ScaleWidth Step R
Line -(i, .RegVal(CDbl(i))), vbBlue \ 2
Next i
End With
End Sub
So wat changes to be made here so that i can perform the action(call the class) wheni click a command button and pass the x and y values as i want it to...
Please help me with this..
I hope i'v made things clear.
Ricky
Re: [HELP] Read from array.
So first i need help in reading a 2 D array..
Then i need to perform the log operation on each of the item in the array...
Then access ach of the values in the 2 newly created array and pass then as x and y values to the liner regression class..
Re: [HELP] Read from array.
Somebody help me out as to how i should read from a 2D array, value by value??
Re: [HELP] Read from array.
BUMP!!!
How to read from a 2D array which has values in it??
Re: [HELP] Read from array.
bumping your threads continously is just going to annoy people.
where is the 2D array in your code?
Re: [HELP] Read from array.
Ya,
But i was so terribly wanting this thing and nobody seems to notice..
I have an array MyArray.. its lbound = 0.. its ubound = 55.. Now this is a sq matrix..
So i just need to read from the array first and then perform log to the base 10 for each of the value read and simultaniously stored in a new array..
Re: [HELP] Read from array.
This is how you dimension the array, then loop through it, then get the value from it. This is assuming you already have data in the array.
VB Code:
Dim astrTestArray(5, 5) As String, strResult As String
For i = 0 To UBound(astrTestArray)
For j = 0 To 5
strResult = astrTestArray(i, j)
Debug.Print strResult
Next j
Next i
Re: [HELP] Read from array.
I have no idea what you're trying to do, but here's an example of reading a 2D array:
VB Code:
Dim MyArray(10, 10) As Long
Dim X As Long, Y As Long
For X = LBound(MyArray, 1) To UBound(MyArray, 1)
For Y = LBound(MyArray, 2) To UBound(MyArray, 2)
Debug.Print X, Y, MyArray(X, Y)
Next Y
Next X
Re: [HELP] Read from array.
Quote:
Originally Posted by vinsanity
Ya,
But i was so terribly wanting this thing and nobody seems to notice..
You cannot expect an answer in a few minutes.
We all have other things to do, and our time here helping people is entirely voluntary. If people can help with your question (and are willing to answer - which is less likely if they see "bumps") they will post as soon as they can - which may take a few hours depending on the circumstances.
Do not bump your threads in less than 24 hours, if at all.
Re: [HELP] Read from array.
The Public functions and properties of the class describe how it interacts with the outside world.
Having given the class a brief look I suppose that you need this function to add data to the object:
VB Code:
Public Function XYAdd(ByVal NewX#, ByVal NewY#)
Dim i&, j&, TX#, Max2O&
Finished = False
Max2O = 2 * GlobalO
TX = 1#
SumX(0) = SumX(0) + 1
SumYX(0) = SumYX(0) + NewY
For i = 1 To GlobalO
TX = TX * NewX
SumX(i) = SumX(i) + TX
SumYX(i) = SumYX(i) + NewY * TX
Next i
For i = GlobalO + 1 To Max2O
TX = TX * NewX
SumX(i) = SumX(i) + TX
Next i
End Function
You call it with NameOfTheObject.XYAdd (X,Y)
As mentioned, you just use two loops to step through the array.
For each element of the array you calculate the X and Y values and pass those to the object with the XYAdd function:
VB Code:
'initialise the object here
For i = LBound(MyArray,1) To UBound(MyArray,1)
For i = LBound(MyArray,1) To UBound(MyArray,1)
RegressionObject.XYAdd (log(0.931227737/MyArray(i,j)), _
log(MyArray(i,j)) )
Next j
Next i
'get the results here
Re: [HELP] Read from array.
Quote:
Originally Posted by si_the_geek
You cannot expect an answer in a few minutes.
We all have other things to do, and our time here helping people is entirely voluntary. If people can help with your question (and are willing to answer - which is less likely if they see "bumps") they will post as soon as they can - which may take a few hours depending on the circumstances.
Do not bump your threads in less than 24 hours, if at all.
I wish to apologize.. I'l make sure I behave...
Re: [HELP] Read from array.
@ jeroen79:
Hello,
i added the function in my code.. Added the class to the project and then do i have to declare : Dim Reg As New RegressionObject
and then in the code u gave,
VB Code:
Private Sub Command1_Click()
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
Reg.XYAdd (log(0.931227737/MyArray(i,j)), log(MyArray(i,j)))
Next j
Next i
End Sub
It displays the 'Reg.XYAdd (log(0.931227737/MyArray(i,j)), log(MyArray(i,j)))' line in red.. and gives error message: compile error Expected=
Re: [HELP] Read from array.
Quote:
Originally Posted by bushmobile
I have no idea what you're trying to do, but here's an example of reading a 2D array:
VB Code:
Dim MyArray(10, 10) As Long
Dim X As Long, Y As Long
For X = LBound(MyArray, 1) To UBound(MyArray, 1)
For Y = LBound(MyArray, 2) To UBound(MyArray, 2)
Debug.Print X, Y, MyArray(X, Y)
Next Y
Next X
I have declared MyArrayY() As Double
Here within the loop why cant i replace the
VB Code:
Debug.Print X, Y, MyAray(X , Y)
and put in
VB Code:
MyArrayY(X, Y) = Log(MyArray(X, Y))
It gives an error message: Subscript out of range..
Re: [HELP] Read from array.
well what have you dimensioned MyArrayY() as. You mention that you have delcared it, well at somepoint you have to dimension it too, i.e. Dim MyArray(10, 10) As Long is a 2D array, 11 elements by 11 elements.
what dimensions have you given MyArrayY()
Re: [HELP] Read from array.
As of now i have used this to write into 2 arrays:
VB Code:
ReDim Preserve MyArrayX(UBound(StrLines) - (LBound(StrLines) + intHeaderLines), UBound(StrWords))
ReDim Preserve MyArrayY(UBound(StrLines) - (LBound(StrLines) + intHeaderLines), UBound(StrWords))
Dim X As Long, Y As Long
For X = 0 To Rowmax - 6
For Y = 0 To Colmax
MyArrayX(X, Y) = Log(0.931227737 / MyArray(X, Y))
MyArrayY(X, Y) = Log(MyArray(X, Y))
Debug.Print X, Y, MyArrayX(X, Y)
Next Y
Next X
I have declared ReDim Preserve as the max values for rows and columns cant be predefined as it depends on the file the user randomly selects..
Now there is no error and the array is working fine..
So now i have 2 separate arrays..
But i made changes to jeroen79's code that he has provided here..
This is wat i have done:
VB Code:
ReDim Preserve MyArrayXY(UBound(StrLines) - (LBound(StrLines) + intHeaderLines), UBound(StrWords))
For i = 0 To Rowmax - 6
For j = 0 To Colmax 'Actual code:
Reg.XYAdd(MyArrayX(i, j), MyArrayY(i, j))
Next j
Next i
But it displays the line:
'Reg.XYAdd(MyArrayX(i, j), MyArrayY(i, j))' in red and gives the error mesage: compile error Expected=
I have included Dim Reg As New RegressionObject..
And the RegressionObject Class is also added..
Wat could be the problem
Re: [HELP] Read from array.
you need to put:
VB Code:
Call Reg.XYAdd(MyArrayX(i, j), MyArrayY(i, j))
'or
Reg.XYAdd MyArrayX(i, j), MyArrayY(i, j)
I think, but can't remember, that it's set up as a function in the class that doesn't return a value (hence it's creating a variant but not doing anything with it) - you might want to change that.
Re: [HELP] Read from array.
Er,
Well wat i need to do now is that i need to perform the linear regression..
y=mx+c.. The class should give the value of m..
That will be done by the class..
So the variables should b passed while calling the class.. And the variables here are- MyArrayX(i,j) and MyArrayY(i,j).. So we must use a for loop to include all the variable pair from the array.. Now the error message is no more there..
This is the code i'v used..
VB Code:
For i = 0 To Rowmax - 6
For j = 0 To Colmax
Reg.XYAdd MyArrayX(i, j), MyArrayY(i, j)
Next j
Next i
So this calls the .XYAd function in the class.. But now i'm not able to figure out how to extract the value of m.. :confused:
Finding the value of 'm' is my objective here..
The RegressionObject.cls file is attached in my first post..
Re: [HELP] Read from array.
okie,
the above code is working and the Reg.Coeff(1) gives the value of 'm'..
It is giving a value: -1.00000000000001
So here it is a negative value.. The slope of the scatter plot would be from top-left to bottom-right..
Now the MyArrayX() and MyArrayY() values must be plotted to in a graph to chk if it gives a -ve curve.. This is to verify if the value of 'm' is correct...
So i guess i need to get this into excel.. Is that possible or is there a better way out??
Re: [HELP] Read from array.
there is a Chart Control that comes with VB6, but I've never used it.
There's also a chart control designed by woka in the CodeBank i think.
Re: [HELP] Read from array.
Hey,
Are there any dlls that come with windows that ccan be used here, so that we can cut down on space used by the application??
Meanwhile I'm trying to figure out how to work with MSChart control..
I need to get data from 2 separate arrays as input for X and Y coordinates to prepare graphs