如何在C#中比较多维数组?

时间:2022-02-19 21:33:53

How to compare multidimensional arrays? Just true/false.

如何比较多维数组?只是真/假。

double[,] data1 = new double[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };    
double[,] data2 = new double[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

//bool compare = data1.SequenceEqual(data2);

Is there way to compare 2d arrays like 1d array ?

有没有办法比较2d数组像1d数组?

data1.SequenceEqual(data2);     

I have to compare every second, so easiest way will be great. Thanks a lot.

我必须比较每一秒,所以最简单的方法将是伟大的。非常感谢。

2 个解决方案

#1


22  

A multidimensional array can be used in linq as one dimensional enumerable. You just need to check also for the length of all dimensions. This snippet should be enough:

多维数组可以在linq中用作一维可枚举。您只需要检查所有尺寸的长度。这个片段应该足够了:

var equal =
    data1.Rank == data2.Rank &&
    Enumerable.Range(0,data1.Rank).All(dimension => data1.GetLength(dimension) == data2.GetLength(dimension)) &&
    data1.Cast<double>().SequenceEqual(data2.Cast<double>());

#2


-4  

You can do this

你可以这样做

data1.SequenceEqual(data2);  

#1


22  

A multidimensional array can be used in linq as one dimensional enumerable. You just need to check also for the length of all dimensions. This snippet should be enough:

多维数组可以在linq中用作一维可枚举。您只需要检查所有尺寸的长度。这个片段应该足够了:

var equal =
    data1.Rank == data2.Rank &&
    Enumerable.Range(0,data1.Rank).All(dimension => data1.GetLength(dimension) == data2.GetLength(dimension)) &&
    data1.Cast<double>().SequenceEqual(data2.Cast<double>());

#2


-4  

You can do this

你可以这样做

data1.SequenceEqual(data2);