VB.Net等同于Matlab的冒号运算符

时间:2018-09-24 09:40:12

标签: vb.net

VB.NET中是否存在一个函数,该函数会生成一维数字数组,该数组指定了起始值,终止值和步长,类似于Matlab的colon operator

如果没有,有人可以建议一种有效的写法吗

getColoumnVector(startVal As Double, step As Double, endVal As Double) As Double ()

谢谢

2 个答案:

答案 0 :(得分:0)

这似乎效果更好,符合预期

    Public Function GetColumnVector(startVal As Double, [step] As Double, endVal As Double) As Double()
    Dim list As New List(Of Double)
    For d = startVal To endVal Step [step]
        list.Add(d)
    Next
    Return list.Concat({endVal}).ToArray()

答案 1 :(得分:0)

冒号运算符的文档显示了如何执行此操作:

  

向量元素大致等于[j,j + i,j + 2 * i,...,j + m i]其中m = fix((k-j)/ i)。但是,如果i不是整数,则浮点算法将在确定冒号是否在向量中包含端点k方面发挥作用,因为k可能不完全等于j + m i

所以:

Function GetColumnVector(startVal As Double, [step] As Double, endVal As Double) As Double()
    Dim nValues = CInt(Math.Ceiling((endVal - startVal) / [step])) + 1
    Dim result(nValues - 1) As Double

    For i = 0 To nValues - 1
        result(i) = startVal + i * [step]
    Next

    Return result

End Function

并且不需要从列表到末尾的数组。