第2个dim数组到1d数组的C#引用 [英] C# reference of 2nd dim array to 1d array

查看:63
本文介绍了第2个dim数组到1d数组的C#引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,可以将一维数组( double [] )作为参数,而我的源数据是一个二维数组( double [,] )像这样的3d点的"cnt"数

I have a function that can take a 1d array (double[]) as an argument while my source data is a 2d array (double[,]) of 'cnt' number of 3d points, like so

double[,] points = new double[3,cnt]

但是我实际上想要做的是将三个第二个暗淡数组中的每一个传递给函数 gpu.Average(double [] arg),最好不必复制按值[i]排列值[i].在C#中有可能吗?

But what I actually want to do is to pass on each of the three 2nd dim arrays to the function gpu.Average(double[] arg), preferably without having to copy the content of the arrays value[i] by value[i]. Is that possible in C#?

示例代码

这有效:

double[] points1d = new double[cnt];
// ... fill points1d with data, then
double a = gpu.Average(points1d);  // <- Alea.gpu.Average() accepts a 1d array 

但是正如我所说,我想将第二维传递给函数gpu.Average(),而不必运行for循环三次即可复制到一维数组中:

But as said, I want to pass on the second dimension to the function gpu.Average() whithout having to run for-loops three times to copy into 1d arrays:

double[,] points2d = new double[3,cnt];
// ... fill points2d with 'cnt' items of data and
// then pass it on

double x = gpu.Average(points2d[0,??]);  // How to pass on 2nd dim?
double y = gpu.Average(points2d[1,??]);     
double z = gpu.Average(points2d[2,??]);     

是否可以不必将数据复制到一维数组中?

Is this possible without having to copy the data into 1d arrays?

(顺便说一句,计算平均值不是使用gpu并行库的好例子,这只是一个比较不同数据类型和结构的执行时间的测试用例).

(BTW, calculating average is not a good example use of gpu parallel library, this is just a test case comparing execution times for different data types & structures).

//罗尔夫(Rolf)

// Rolf

推荐答案

在托管代码中,如果不复制例如以下内容,这是不可能的. points2d [1,*] 到一个新的一维数组.

No this is not possible in managed code without copying the contents of e.g. points2d[1,*] to a new single dimensional array.

在托管代码中,每个数组都必须知道其长度,以便可以检查每个索引访问,并在索引超出范围时抛出 ArrayIndexOutOfBounds 异常.长度(整数值)存储在数组的第一个元素之前,以便运行时知道在哪里可以找到它.现在,我想这可以理解为什么您不能将数组拆分为几个部分,而将它们视为新数组.请注意,对于多维数组,这个故事要复杂得多,因为多维数组必须存储维数和每个维的大小...

In managed code every array has to know its length, so that every index access can be checked and ArrayIndexOutOfBounds exception thrown if the index is out of bounds. The length (integer value) is stored before the first element of the array, so that the runtime knows where to find it. Now with this I guess one can see why you cannot split array into several parts and treat those as new arrays. Note that the story is even more complicated for multidimensional arrays, which have to store the number of dimensions and the size of each dimension...

您可以做的是使用锯齿状数组:

What you can do is to use jagged array:

double[][] points2d = new double[3][];
points2[0] = new double[cnt];
...
gpu.Average(points2d[0]);

锯齿状数组比多维数组快,建议使用多维数组代替多维数组

Jagged arrays are faster than multidimensional arrays and are recommended to be used instead of multidimensional arrays even by MS.

这篇关于第2个dim数组到1d数组的C#引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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