vb.net - 如何从 Mathnet、vb.net 中的矩阵或向量中找到实体索引?
问题描述
假设我有一个矩阵 A=[1 2 3;4 5 6] 现在我想检查其中是否存在 5。答案应该是行和列,即 Row=1 Col=1 我试过 find 但没有发现它有用。
提前致谢
解决方案
对于多维数组,我们的选择很糟糕。一种方法是循环数组以获取元素索引,或者尝试类似下面的代码,仅循环搜索的数字匹配。
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
用法:
Dim indexes As List(Of GridData) = FindIndexesOfNumber(5)
推荐阅读
- java - 如何在 Android TimeUnit 中获取经过的时间
- karate - 如何在空手道功能文件中调用 java 类?
- android - ConstraintLayout 不处理点击
- elasticsearch - 如何从 Elasticsearch 返回源字段
- flutter - 检查列表键的名称并更改它
- javascript - 如何上传或拖放图像
- javascript - 使用函数参数作为变量调用
- flutter - 通过提供者创建无限滚动的 ListView:在构建期间调用 setState() 或 markNeedsBuild()
- php - phpMyAdmin 执行查询比普通 PHP 脚本更快
- python - 使用 worksheet.write 更新单元格时出现 xlsxwriter 错误