C# 如何运行逻辑';xnor';跨多维数组中的列(linq或简单算法)?

C# 如何运行逻辑';xnor';跨多维数组中的列(linq或简单算法)?,c#,arrays,algorithm,linq,logical-and,C#,Arrays,Algorithm,Linq,Logical And,目前,我有一系列的可能性,我希望根据这些可能性来计算事实(逻辑XNOR) 我试图将其转换为XNOR的布尔数组(所有值相等) 预期结果: [ 0, 1, 1, 1, 1, 1, 1, 0 ] 可能性的数目是1。。N另一个例子是: var possibilities = new[] { new[] {1, 1, 0, 1, 1, 0, 0, 0}, new[] {1, 1, 0, 1, 1, 0, 0, 1}, new[] {0, 1, 1, 0, 1, 1, 0, 1}

目前,我有一系列的可能性,我希望根据这些可能性来计算事实(逻辑XNOR)

我试图将其转换为XNOR的布尔数组(所有值相等)

预期结果:

[ 0, 1, 1, 1, 1, 1, 1, 0 ]
可能性的数目是1。。N另一个例子是:

var possibilities = new[] {
    new[] {1, 1, 0, 1, 1, 0, 0, 0},
    new[] {1, 1, 0, 1, 1, 0, 0, 1},
    new[] {0, 1, 1, 0, 1, 1, 0, 1}
}
[ 0, 1, 0, 0, 1, 0, 1, 0 ]
结果是:

var possibilities = new[] {
    new[] {1, 1, 0, 1, 1, 0, 0, 0},
    new[] {1, 1, 0, 1, 1, 0, 0, 1},
    new[] {0, 1, 1, 0, 1, 1, 0, 1}
}
[ 0, 1, 0, 0, 1, 0, 1, 0 ]
我最初的方法是开始构建嵌套循环并迭代每种可能性,获取索引并进行比较,但这似乎非常“黑客”,我相信有一种更简单(更好)的方法通过LINQ处理它(但我不太了解LINQ来编写)或者是一种不需要难看的嵌套循环的算法

编辑#1:使用了不正确的“逻辑”术语


正如下面的回答中提到的,正确的逻辑实际上不是XNOR,而是XNOR(所有列都相等)。事实上,当我(如上所述)真的在寻找所有值相等的结果1(或true)时,如果所有列都是0,则会产生0。

您正在寻找zip扩展名。但您需要使用聚合来处理两个以上的问题。唯一的差异是结果为1的三个零。and运算符(&)将为零

    static void Main(string[] args)
    {
        var possibilities = new[]
        {
            new[] {0, 1, 1, 1, 1, 1, 1, 1},
            new[] {1, 1, 1, 1, 1, 1, 1, 0}
        };
        //[ 0, 1, 1, 1, 1, 1, 1, 0 ]
        var result = possibilities.Aggregate((f, s) => f.Zip(s, (fi, si) => fi & si).ToArray());
        var possibilities2 = new[]
        {
            new[] {1, 1, 0, 1, 1, 0, 0, 0},
            new[] {1, 1, 0, 1, 1, 0, 0, 1},
            new[] {0, 1, 1, 0, 1, 1, 0, 1}
        };
        //[ 0, 1, 0, 0, 1, 0, 1, 0 ]
        var result2 = possibilities2.Aggregate((f, s) => f.Zip(s, (fi, si) => fi & si).ToArray());

        Console.ReadLine();
    }
并使用循环:

int j = 0, size = possibilities[0].Length;
int[] result = new int[size];
for (int i = 0; i < size; i++)
{ 
     for (j = 0; j < possibilities.Length; j++)
         if (possibilities[j][i] == 0) { result[i] = 0; break; }
     if (j == possibilities.Length) result [i] = 1;
}
带循环的XNOR:

int size = possibilities[0].Length;
int[] result = new int[size];
for (int i = 0; i < size; i++)
{ 
     int q = possibilities[0][i];
     for (j = 1; j < possibilities.Length; j++)
         if (possibilities[j][i] != q) { result[i] = 0; break; }
     if (j == possibilities.Length) result[i] = 1;
}
int size=可能性[0]。长度;
int[]结果=新的int[大小];
对于(int i=0;i

这里最简单的实现是这样说:

for(i = 0; i < list1.Count(); i++){
list3[i] = list1[i] && list2[i]
}
for(i=0;i
如果必须使用linq实现此功能,则可以导入MoreLinq库,然后将.interleave与.batch合并


这里有一种方法,只使用1个循环来携带相关元素的索引

static void Main(string[] args)
{
    var possibilities = new[] {
        new[] {1, 1, 0, 1, 1, 0, 0, 0},
        new[] {1, 1, 0, 1, 1, 0, 0, 1},
        new[] {0, 1, 1, 0, 1, 1, 0, 1}
    };

    IList<int> output = new List<int>();

    for (int i = 0; i < possibilities[0].Length; i++)
    {
         output.Add(possibilities.All(x => x.ElementAt(i) == possibilities[0][i]) ? 1 : 0);
    }

    Console.WriteLine("[{0}]", string.Join(", ", output));

    Console.ReadKey();
}
static void Main(字符串[]args)
{
var可能性=新[]{
新[]{1,1,0,1,1,0,0,0},
新[]{1,1,0,1,1,0,0,1},
新[]{0,1,1,0,1,1,0,1}
};
IList输出=新列表();
对于(int i=0;i<可能性[0]。长度;i++)
{
输出.Add(可能性.All(x=>x.ElementAt(i)=可能性[0][i])?1:0);
}
Console.WriteLine(“[{0}]”,string.Join(“,”,output));
Console.ReadKey();
}
第一个示例返回[0,1,1,1,1,1,1,0]。
返回[0,1,0,0,1,0,1,0,1,0]作为第二个示例。

每个可能性的大小是否固定?e、 g.您在上面的每个示例中都显示了8。@MikeH啊,是的,大小是固定的,因为每一行可能都是相同的长度(但是在不同的执行上下文中它们可能是不同的长度)。尽管阿什坎·莫巴耶恩·基亚巴尼(Ashkan Mobayen Khiabani)的林克(linq)也比这更优雅,但OP指出:“可能性的数量是1..n”。你的算法不适用于任意数量的列表。我知道在LINQ中有一种更干净的方法。现在我有了它,我可以把它分解,同时学习LINQ。@AaronMurray很高兴它也帮助了+1的循环添加,我知道/听说LINQ有一些性能上的问题,在我的情况下,我怀疑这不是问题,可能性数据集相对较小,但是未来的项目可能需要非LINQ解决方案。@AaronMurray谢谢,在大多数情况下,性能差异并不明显,但在大多数情况下,linq将非常复杂的代码转换为非常简单的代码!您是正确的,我应该引用的逻辑运算符是XNOR(集合中的所有值都相等),在场景中,0&0实际上是0,其中(XNOR)a==b==n?1:0。。。那么,当您一次比较两个数组时,这将不起作用。尽管如此,感谢您的回答,因为OP使用了一个不正确的逻辑术语,这导致了混乱,我已经更新了问题以澄清问题。@clayton re:更好的答案(删除注释),我只是先看到了Askan的答案(不知何故,因为您实际上是先回答的),我对它进行了测试,结果成功了:)我也投了你一票,因为你的回答很有帮助,而且现在也给了我更多的研究,是的appreciated@AaronMurray是的,我认为在目前的情况下,它更好。我不想表现得太咸。我什么都不该说。
static void Main(string[] args)
{
    var possibilities = new[] {
        new[] {1, 1, 0, 1, 1, 0, 0, 0},
        new[] {1, 1, 0, 1, 1, 0, 0, 1},
        new[] {0, 1, 1, 0, 1, 1, 0, 1}
    };

    IList<int> output = new List<int>();

    for (int i = 0; i < possibilities[0].Length; i++)
    {
         output.Add(possibilities.All(x => x.ElementAt(i) == possibilities[0][i]) ? 1 : 0);
    }

    Console.WriteLine("[{0}]", string.Join(", ", output));

    Console.ReadKey();
}