C :将矢量重新为3D数组
C++: Reshape vector to 3D array
编辑:我已将向量上传为文本文件,以防万一任何人要查看:https://drive.google.com/file.com/file/d/0b0b0b0wspu8yebrqbduwnfyza3ljsnc/查看?usp =共享
我正在尝试将矢量h
重塑为3D数组。h
包含295788个元素。在这种情况下,height = 314
,width = 314
和depth = 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=7
。h
是具有295788个元素的向量。nrh=314
,nch=314
和nradii=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((
的解决方案#2int 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
增量且相同。显示此计算很容易读取每个元素一次。
我通常会期望width
,height
,然后是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
中的顺序,则需要更改i
,j
和k
的位置,并相应地调整因素。
将其放在一起,您的作业循环应读取:
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一样动态大小的数组类型。
- 如何计算 3d 数组中的索引值在内存中的位置?如何计算字符**中的索引值在内存中的位置?
- 我是 c++ 的新手,有没有一种算法可以找到 3d 数组中最接近的 0 的距离?
- 如何用零填充 3D 数组?
- 将长度可变的 C99 静态 3D 数组传递给函数
- C :将矢量重新为3D数组
- 将动态 3D 数组发送到 C++ 中的函数
- C++ 3D 数组到 1D 会导致堆缓冲区溢出
- 初始化const 3D数组成员变量
- 处理3D数组的边缘和角案例的方法
- 将2D数组的实例分配给3D数组中的索引
- 使用 vector 在 c++ 中声明 3D 数组结构
- 为什么C CHAR数据类型3D数组通过参数第一个括号为空白
- 可以使用 std::copy 将 3D 数组复制到矢量
- 我的重载 [] 运算符不适用于伪 3D 数组
- 从 3D 数组转换为 1D 数组失败
- 如何将3D .OBJ对象表示为3D数组
- 如何在C++中定义每个维度具有不同类型的 3D 数组
- CPP 3D 动态创建由单个内存块组成的 3D 数组
- 视觉读取列文本数据并在C++中放入 3D 数组
- 将 3D 数组的索引传递给函数