C :将矢量重新为3D数组

C++: Reshape vector to 3D array

本文关键字:3D 数组      更新时间:2023-10-16

编辑:我已将向量上传为文本文件,以防万一任何人要查看:https://drive.google.com/file.com/file/d/0b0b0b0wspu8yebrqbduwnfyza3ljsnc/查看?usp =共享

我正在尝试将矢量h重塑为3D数组。h包含295788个元素。在这种情况下,height = 314width = 314depth = 3。基本上,我要做的是Matlab对其重塑功能所做的。

h = reshape(h, height, width, depth)

这是我到目前为止的尝试,但是当我打印它时,我看到的全部都是零,这是不对的。我仔细检查了h包含我期望的数字。

vector<vector<vector<double> > > array3D;
int height = 314, width = 314, depth = 3;
// Set up sizes
array3D.resize(height);
for (int i = 0; i < height; ++i) {
    array3D[i].resize(width);
    for (int j = 0; j < width; ++j)
        array3D[i][j].resize(depth);
}
for (int i = 0; i < height; i++)
{
    array3D[i][0][0] = h[i];
    for (int j = 0; j < width; j++)
    {
        array3D[i][j][0] = h[i+j];
        for (int k = 0; k < depth; k++)
        {
            array3D[i][j][k] = h[i+j+k];
        }
    }
}

打印:

for (vector<vector<vector<double>>>::const_iterator i = array3D.begin(); i != array3D.end(); ++i)
{
    for (vector<vector<double>>::const_iterator j = i->begin(); j != i->end(); ++j)
    {
        for (vector<double>::const_iterator k = j->begin(); k != j->end(); ++k)
        {
            cout << *k << ' ';
        }
    }
}

所以我的问题是,如何正确地将矢量转换为3D数组?

我设法通过亨利·梅克(Henri Menke(建议使用eigen :: tensor来执行此操作。我最终为最初的314x314x3矩阵创建了一个数组,然后为300x300x3矩阵创建了一个数组。它既不快,也不是漂亮,但是现在这就是我想到的。看起来像这样。

进行澄清:margin是在代码中进一步计算的,但是在此示例中,使用314x314x3矩阵它的margin=7h是具有295788个元素的向量。nrh=314nch=314nradii=3

Tensor<int, 3> t(nrh, nch, nradii);
int counter = 0;
for (int k = 0; k < nradii; k++)
{
    for (int col = 0; col < nch; col++)
    {
        for (int row = 0; row < nrh; row++) 
        {
            t(row, col, k) = h[counter];
            counter += 1;
        }
    }
}
int height = nrh - margin * 2;
int width = nch - margin * 2;
int depth = nradii;
Tensor<int, 3> out(height, width, depth);
int count1 = 0, count2 = 0, count3 = 0;
for (int k = 0; k < depth; k++)
{
    for (int j = margin; j < nch - margin; j++)
    {
        for (int i = margin; i < nrh - margin; i++)
        {
            out(count1, count2, count3) = t(i, j, k);
            count1 += 1;
        }
        count1 = 0;
        count2 += 1;
    }
    count2 = 0;
    count3 += 1;
}

编辑:tensor.slice((

的解决方案#2
int height = nrh - margin * 2;
int width = nch - margin * 2;
int depth = nradii;
Tensor<int, 3> tensor(height, width, depth);
DSizes<ptrdiff_t, 3> indices(margin, margin, 0);
DSizes<ptrdiff_t, 3> sizes(height, width, nradii);
tensor = t.slice(indices, sizes);

怎么样:

array3D[i][j][k] = h[i*(depth*width)+j*depth+k];

可能会以正确的顺序扫描向量的

请注意,索引k如何重置索引j递增索引,以便您精确地移动一个,直到索引j重置在这种情况下i增量且相同。显示此计算很容易读取每个元素一次。

我通常会期望widthheight,然后是depth,您的扫描顺序相反!

脚注:取决于应用程序,可能值得只使用此方法访问向量。通常,事实证明,它比访问向量向量的向量要快。在处理大型阵列时,这可能很重要。

实际上,您的代码结构已经可以,但是有两个错误:

  • 线

    array3D[i][0][0] = h[i];
    

    array3D[i][j][0] = h[i+j];
    

    是毫无意义的。您稍后用该行覆盖这些条目

    array3D[i][j][k] = h[i+j+k];
    
  • h[]的索引计算是错误的:在添加单元格索引之前,您必须将行索引乘以行的长度。作业应该像这样:

    array3D[i][j][k] = h[(i*width+j)*depth+k];
    

    否则,您将获得(i, j, k) == (3, 2, 1)(i, j, k) == (1, 3, 2)相同的结果,这显然是错误的。在上面的索引计算中,我假设k是最快的变化维度。如果这不是将数据存储在h中的顺序,则需要更改ijk的位置,并相应地调整因素。

将其放在一起,您的作业循环应读取:

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        for (int k = 0; k < depth; k++) {
            array3D[i][j][k] = h[(i*width+j)*depth+k];
        }
    }
}

略有主题:
如果您使用的是C ,则可以"简单地"这样做:

size_t dataSize;
//Create a real 3D array with the dimensions (height, width, depth).
double (*array3D)[width][depth] = malloc(dataSize = height*sizeof(*array3D));
//Copy over the data from the file.
memcpy(array3D, h, dataSize);

//Print the array contents:
for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        for (int k = 0; k < depth; k++) {
            printf("%d ", array3D[i][j][k]);
        }
    }
}

这使用一个真实的3D阵列,而不是一系列指针,将指针阵列阵列到双打阵列(大约是vector<vector<vector<double>>>是什么(。但是,这不能在C 中完成,因为C 不允许像C一样动态大小的数组类型。