如何检查多维数组行是否包含非零值 [英] How to check if multidimensional array row contains non-Zero value
问题描述
关于 C# 中多维数组的一个简短问题.
如何检查多维数组的一行是否包含非零值?在 Matlab 中,any"命令完全符合我的需要.
最后,我需要将请求置于 while 条件中.Matlab 中的意思:while(any(array[1,2,:])) --> 意思是:while(any-entry-of-the-row-is-not-Zero)...
我已经尝试过 Array.Exists() 或 Array.Find() 但它似乎只适用于一维数组.
谢谢
您有几个选择
myMultiArray.Any(row => row.Contains(Something));
或按照 Sriram Sakthivel 的建议
foreach(myMultiArray 中的 var 行)if(row.Contains(Something)//找到了!foreach(myMultiArray 中的 var 行)if(row.IndexOf(Something) >= 0)//找到了!
更具体地说是您的问题
myMultiArray.Any(row => row.Any(cell => cell != 0));foreach(myMultiArray 中的 var 行)foreach(myMultiArray 中的 var 单元格)如果(单元格!= 0)//找到了!for(int i = 0; i
MSDN 信息
任何>
Just a short question regarding multidimensional arrays in C#.
How can I check if one row of a multidimensional array contains a non-Zero value? In Matlab, the "any"-command does exactly what I need.
Finally I need to put the request into a while condition. Means in Matlab: while(any(array[1,2,:])) --> which means: while(any-entry-of-the-row-is-not-Zero)...
I tried already the Array.Exists() or Array.Find() but it seems to work only with one-dimensional arrays.
Thanks
You have a couple options
myMultiArray.Any(row => row.Contains(Something));
or as Sriram Sakthivel Suggested
foreach(var row in myMultiArray)
if(row.Contains(Something)
//Found it!
foreach(var row in myMultiArray)
if(row.IndexOf(Something) >= 0)
//Found it!
More spefically to your question
myMultiArray.Any(row => row.Any(cell => cell != 0));
foreach(var row in myMultiArray)
foreach(var cell in myMultiArray)
if(cell != 0)
//Found it!
for(int i = 0; i < array.GetLength(0); i++)
for(int j = 0; j < array.GetLength(1); j++)
if(array[i,j] != 0)
//Do Something
MSDN Information
这篇关于如何检查多维数组行是否包含非零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!