C#将3D矩阵展平/扩展为锯齿状数组 [英] C# flattening/expanding a 3D matrix into a jagged array

查看:79
本文介绍了C#将3D矩阵展平/扩展为锯齿状数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须弄平3d数组才能进行序列化. 让我们从这里开始:

I have to flatted a 3d array in order to be serialized. Let's start with this:

int[,,] array3D = new int[,,] { 
            { { 1, 2 }, { 3, 4 }, {5,6 }, {7,8 } },
            { { 9, 10}, { 11, 12},{ 13,14} , {15,16 }},
            { { 17, 18}, { 19, 20},{ 21,22}, {23,24 } }
        };

使它像这样(类似1,2,3,4,...,24):

which makes it like this (something like 1,2,3,4,...,24):

所以我现在有这个s/r

So now I have this s/r

public static T[] Flatten<T>(T[,,] arr)
{
    int rows0 = arr.GetLength(0);
    int rows1 = arr.GetLength(1);
    int rows2 = arr.GetLength(2);
    T[] arrFlattened = new T[rows0 * rows1* rows2];

    int i, j, k;
    for (k = 0; k < rows2; k++)
    {
        for (j = 0; j < rows1; j++)
        {
            for (i = 0; i < rows0; i++)
            {
                var test = arr[i, j, k];
                int index = i + j * rows0 + k * rows1;
                arrFlattened[index] = test;
            }
        }
    }
    return arrFlattened;
}

将3d矩阵展平为1d数组

which flattens the 3d matrix into a 1d array

我不够聪明,无法理解该程序是否正确,但让我们进一步. 然后,使用以下s/r

I am not smart enough to understand if the procedure is correct but let's go further. I then Expand with the following s/r

public static T[,,] Expand<T>(T[] arr, int rows0, int rows1)
{
    int length = arr.GetLength(0);
    int rows2 = length / rows0 / rows1;

    T[,,] arrExpanded = new T[rows0, rows1, rows2];
    for (int k = 0; k < rows2; k++)
    {
        for (int j = 0; j < rows1; j++)
        {
            for (int i = 0; i < rows0; i++)
            {
                T test = arr[i + j * rows0 + k * rows1];
                arrExpanded[i, j, k] = test;
            }
        }
    }
    return arrExpanded;
}

但结果如下:

所以没有像1,2,3,4,5 .... 24 我知道该错误可能是微不足道的,但是请尝试一下,因为我可能找不到它. 预先感谢.

So nothing like 1,2,3,4,5....24 I know that the error might be a trivial one but try as I might I can't find it. Thank in advance.

帕特里克

感谢所有这3个解决方案的帮助,它们非常出色而且可以正常工作,但是我选择了一种更容易理解和调试的解决方案

Thanks for helping all 3 solution were amazing and working but I chose the one more easily to understand and debug for me

推荐答案

我假设您想知道代码中的错误是什么,而不是想要了解最快的答案.您的索引计算错误.您正在这样计算:

I am assuming that you want to know what is the mistake in your code more than you want to know the quickest way to get to the answer. Your index calculation is wrong. You are calculating it like this:

int index = i + j * rows0 + k * rows1;

但实际上,您不仅需要将最后一项乘以row1,还应乘以row0:

But you actually need to multiply the last term not just by rows1 but by rows0 too:

int index = i + j * rows0 + k * rows1 * rows0;

此外,交换在for循环中迭代的维度顺序以按顺序获得结果也很有意义.最终的代码将是:

Also, it makes sense to swap the order of dimensions that are iterated in for loops to get results in order. The final code for that would be:

    public static T[] Flatten<T>(T[,,] arr)
    {
        int rows0 = arr.GetLength(0);
        int rows1 = arr.GetLength(1);
        int rows2 = arr.GetLength(2);
        T[] arrFlattened = new T[rows0 * rows1* rows2];

        int i, j, k;
        for (k = 0; k < rows0; k++)
        {
            for (j = 0; j < rows1; j++)
            {
                for (i = 0; i < rows2; i++)
                {
                    var test = arr[k, j, i];
                    int index = i + j * rows2 + k * rows1 * rows2;
                    arrFlattened[index] = test;
                }
            }
        }
        return arrFlattened;
    }

    public static T[,,] Expand<T>(T[] arr, int rows0, int rows1)
    {
        int length = arr.GetLength(0);
        int rows2 = length / rows0 / rows1;

        T[,,] arrExpanded = new T[rows0, rows1, rows2];
        int i, j, k;
        for (k = 0; k < rows0; k++)
        {
            for (j = 0; j < rows1; j++)
            {
                for (i = 0; i < rows2; i++)
                {
                    T test = arr[i + j * rows2 + k * rows1 * rows2];
                    arrExpanded[k, j, i] = test;
                }
            }
        }
        return arrExpanded;
    }

这篇关于C#将3D矩阵展平/扩展为锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆