如何映射一个三维阵列线性阵列? [英] How to map a 3D array to a linear array?

查看:92
本文介绍了如何映射一个三维阵列线性阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个三维数据阵列重新presents 2D数据的堆叠。在这种特殊情况下,栈仅包含一个元素。
什么是该数据映射到线性阵列的最快方法?

I have a 3D data array which represents a stack of 2D data. In this particular case, the stack only contains one element. What is the fastest way to map this data to a linear array?

我正打算采取以下办法:

I was planning to take the following approach:

public static int[] ToBuffer<(this int[, ,] buffer3D, int w, int h)
  {
      int[] buffer = new int[w * h * 1];

      for (int i = 0; i < (w * h); i++)
      {
          buffer[i] = buffer3D[(int)Math.Floor(i / (double)w), i % w, 0];
      }

      return buffer;
   }

但它发生,我认为可能有一些方法来充分利用数据的方式已经存储在内存中。是否有应CSHARP采用一些其他的映射方法?

But it occurred to me that there might be some way to take advantage of how the data is already stored in memory. Is there some other mapping approach that should be employed in csharp?

推荐答案

我猜想,开创了新的数组新的缓冲区和复制三维阵列的缓冲应该工作:

I guess, creating new buffer for the new array and copying the buffer of the 3d-array should work:

public static int[] ToBuffer(int[, ,] buffer3D, int w, int h) {
    int[] buffer = new int[w * h * 1];
    Buffer.BlockCopy(buffer3D, 0, buffer, 0, w * h * sizeof(int));
    return buffer;
}

这篇关于如何映射一个三维阵列线性阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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