将一维的“平坦”索引转换为n维数组的n维矢量索引。

时间:2022-08-11 21:43:00

I have an N-dimensional array, with the same number of items (i.e. the same "length") in each dimension.

我有一个n维数组,每个维度都有相同数量的项(即相同的“长度”)。

Given a one-dimensional index into the array, I want a function that returns the coordinates associated with that index. The way that the array is indexed actually doesn't matter (in the sense that all of the dimensions of the array are equal, with none having precedence in terms of algorithms that will be operating on the array).

给定数组中的一维索引,我想要一个返回与该索引相关联的坐标的函数。数组被编入索引的方式实际上并不重要(从数组的所有维度都是相等的意义上来说,对于将在数组上操作的算法来说,没有一个维度具有优先级)。

So, for example, if I have a 4x4x4 array, index 63 should return [3,3,3], index 0 should return [0,0,0] and index 5 should return [1,1,0].

因此,例如,如果我有一个4x4x4的数组,索引63应该返回[3,3,3],索引0应该返回[0,0,0],而索引5应该返回[1,1,0]。

I have written the following function, where nDim is the number of dimensions, and nBin is the length of each dimension:

我写了如下的函数,其中nDim是维度的个数,nBin是每个维度的长度:

def indicesOf(x,nDim,nBin) :
    indices = []
    for i in arange(0,nDim) :   
        index = (x/nBin**(i))%nBin
        indices.append(index)
        x -= index*nBin**i
    return indices

It seems to work -- but is there a more efficient way to make this calculation? To be honest, I have half "asked" this question just to share this solution, as I couldn't find a solution online. But if there is a more efficient way to do this, then great -- please share!

这似乎是可行的——但有没有更有效的方法来进行这种计算呢?老实说,我有一半的问题是为了分享这个解决方案,因为我在网上找不到解决方案。但如果有一种更有效的方法,那很好——请分享!

The function above is written in python, but I have just been using this to prototype a C (actually CUDA) function, so none of pythons wonderful libraries are available to me.

上面的函数是用python编写的,但我只是用它来原型化一个C(实际上是CUDA)函数,所以我不能使用python的奇妙库。

Here is a solution from combining JackO* and Eric's comments about the power of two sizes below. It seems to work for the handful of test cases I tried.

这里有一个解决方案,结合JackO*和Eric关于下面两个尺寸的力量的评论。它似乎为我尝试过的少数测试用例工作。

def indicesPowOf2(x,nDim,nBin) :
    logWidth = math.log(nBin,2)         
    indices = [0]*nDim
    for i in arange(nDim) :
        indices[i] = x & (nBin-1)
        x = x >> int(logWidth)
    return indices

1 个解决方案

#1


2  

You could avoid using ** (power operator) to reduce the computational cost, especially if you need to add this code to CUDA kernels.

您可以避免使用** (power操作符)来降低计算成本,特别是如果您需要将此代码添加到CUDA内核中。

The following way could be more efficient

下面的方法可能更有效。

void indices(int x, int nDim, int nBin, int indices[]) {
    for(int i=0;i<nDim;i++) {
        indices[i] = x % nBin;
        x /= nBin;
    }
}

If your nBin is a power of 2, you could use >> and & to replace the / and %.

如果nBin是2的幂,可以使用>>和&替换/和%。

#1


2  

You could avoid using ** (power operator) to reduce the computational cost, especially if you need to add this code to CUDA kernels.

您可以避免使用** (power操作符)来降低计算成本,特别是如果您需要将此代码添加到CUDA内核中。

The following way could be more efficient

下面的方法可能更有效。

void indices(int x, int nDim, int nBin, int indices[]) {
    for(int i=0;i<nDim;i++) {
        indices[i] = x % nBin;
        x /= nBin;
    }
}

If your nBin is a power of 2, you could use >> and & to replace the / and %.

如果nBin是2的幂,可以使用>>和&替换/和%。