Search code examples
vb.netmathnet-numerics

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. The answer should be the row and column i.e Row=1 Col=1 I have tried find but not finding it useful.

Thanks in advance


Solution

  • 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