减去Array vb.net的元素 [英] Subtract elements of Array vb.net
问题描述
我正在研究数组而且我遇到了问题。我正在尝试计算数据点Xi和它的前身Xi-1之间的差异。
例如,数组或列表中的每个元素都需要用前一个元素减去。
系列
1
2
4
8
3
3
3
4
2
2
。
。
所需产量
1
2
4
5
0
0 >
1
2
0
我试过使用两个阵列......我正在使用图表,但想法是一样的..我试图分离元素,但我不能解决。我是stuct ..任何例子或任何东西都会有所帮助.. thanx提前
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
推荐答案
我不确定temp
或代码示例中的ListBox
表示此解决方案对您的问题无效,但以下代码将初始化arrayA
与您的第一个系列,然后计算arrayB
,其中包含您想要的结果集。
I''m not sure iftemp
or theListBox
s in your code example mean that this solution isn''t valid for your problem, but the following code will initialisearrayA
with your first series and then calculatearrayB
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
我在计算过程中添加了一个 Math.Abs
调用,因为你需要的输出集合不包含任何底片。
I added a Math.Abs
call during the calculation as your required output set doesn''t contain any negatives.
这篇关于减去Array vb.net的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!