[英]How to find entity index from matrix or vector in Mathnet, vb.net?
Lets say i have an matrix A=[1 2 3;4 5 6] Now i want to check whether 5 is present in it or not.假设我有一个矩阵 A=[1 2 3;4 5 6] 现在我想检查其中是否存在 5。 The answer should be the row and column ie Row=1 Col=1 I have tried find but not finding it useful.答案应该是行和列,即 Row=1 Col=1 我试过 find 但没有发现它有用。
Thanks in advance提前致谢
With multidimensional arrays we have poor choices.对于多维数组,我们的选择很糟糕。 One way is that you loop your array to get element indexes or try something like the code below which loop only your searched number matches.一种方法是循环数组以获取元素索引或尝试类似下面的代码,该代码仅循环匹配您搜索的数字。
ReadOnly matrix(,) As Integer = New Integer(1, 2) {{1, 2, 5}, {4, 5, 6}}
Structure GridData
Dim Row As Integer
Dim Col As Integer
End Structure
Function FindIndexesOfNumber(searchedNr As Integer) As List(Of GridData)
Dim startIndex As Integer = 0
Dim enumerator As List(Of GridData) = (From elements In matrix).ToList.Where(Function(x) x.Equals(searchedNr)).Select(Function(x)
'fake select, it's only for the loop which find numbers
startIndex = Array.IndexOf(matrix.OfType(Of Integer)().ToArray(), searchedNr, startIndex + 1)
Dim matrixBound As Integer = matrix.GetUpperBound(1) + 1
Return New GridData With {
.Col = startIndex Mod matrixBound,
.Row = (startIndex \ matrixBound)
}
End Function).ToList
For Each c In enumerator
Console.WriteLine(String.Format("Col: {0} Row: {1}", c.Col.ToString, c.Row.ToString))
Next
Return enumerator
End Function
Usage:用法:
Dim indexes As List(Of GridData) = FindIndexesOfNumber(5)
You can try this你可以试试这个
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim a As Integer(,) = New Integer(,) {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}, {12, 13, 14}}
Dim rw As Integer, cl As Integer
Dim xy As Integer = 13
If getXY(a, xy, rw, cl) = True Then
MessageBox.Show("Row: " & rw & " # Col: " & cl)
Else
MessageBox.Show("Not found")
End If
End Sub
Function getXY(arr As Array, findwhat As Object, ByRef row As Integer, ByRef col As Integer) As Boolean
Dim d = (From x In arr).ToList.IndexOf(findwhat)
If d = -1 Then Return False
row = Math.Ceiling((d + 1) / arr.GetLength(1)) - 1
col = d Mod arr.GetLength(1)
Return True
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.