Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: list VB.NET array
Hi, I am working on arrays and I have a problem.I am trying to calculate the difference between data point,Xi, and its predecessor,Xi-1.
For example each element in an array or list needs to be subtracted with the previous element.
 
Series
1
2
4
8
3
3
3
4
2
2
.
.
 
Required output
1
2
4
5
0
0
1
2
0
 
I have tried to use two arrays ...I am using charts but the idea is the same..I tried to seperate the elements but I cant work out. I am stuct.. any example or anything would help.. thanx in advance
 
For i = 0 To Chart1.Series("series1").Points.Count - 1
          temp = Chart1.Series("series1").Points(i).YValues
          xArray(i) = temp(0)
          ListBox1.Items.Add(xArray(i).ToString())
 
      Next
 

      For i = 1 To Chart1.Series("series1").Points.Count - 1
          temp = Chart1.Series("series1").Points(i).YValues
          yArray(i) = temp(0) - temp(i)
          ListBox2.Items.Add(yArray(i).ToString())
          i += 1
      Next
 

Thanks in advance..
Cheers
Posted 21-Nov-12 7:13am
Comments
Sergey Alexandrovich Kryukov at 21-Nov-12 12:50pm
   
Do you understand that giving an example does not mean definition of the problem? For arbitrary X[0], what's its "predecessor"? 0? X[i]-1 is not difference between members, its X[i]-1; did you mean X[i-1]? We could use your code for definition, but you complain that it's wrong... Indeed, it look gibberish. Why "ToString()"?! Do you want to work with data, or its string presentation. What is temp? why? Why you always use temp(0), which is not "predecessor"?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I'm not sure if temp or the ListBoxs in your code example mean that this solution isn't valid for your problem, but the following code will initialise arrayA with your first series and then calculate arrayB which contains your desired result set.
 
        Dim arrayA As Integer() = {1, 2, 4, 8, 3, 3, 3, 4, 2, 2}
        Dim arrayB(arrayA.Length - 2) As Integer
 
        For i As Integer = 1 To arrayA.Length - 1
            arrayB(i - 1) = Math.Abs(arrayA(i) - arrayA(i - 1))
        Next
 
I added a Math.Abs call during the calculation as your required output set doesn't contain any negatives.
  Permalink  
Comments
sana chan at 22-Nov-12 6:48am
   
Thanks a lot. this is all the code i have
 
Dim xArray(Chart1.Series("series1").Points.Count) As Double
Dim yArray(Chart1.Series("series1").Points.Count) As Double
Dim zArray(Chart1.Series("series1").Points.Count) As Double
 
Dim temp() As Double
 
Dim x As New ArrayList
 
For i = 0 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
xArray(i) = temp(0)
ListBox1.Items.Add(xArray(i).ToString())
 
Next
 

For i = 0 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
yArray(i) = temp(0)
ListBox2.Items.Add(yArray(i).ToString())
i += 1
Next

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 240
2 CPallini 195
3 Maciej Los 177
4 BillWoodruff 173
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100