如何旋转向量的向量
我正在寻找一种优雅的方法来旋转向量的向量,最好使用STL算法或boost 示例数据看起来像这样
vector<vector<int> > vm;
vector<int> v;
v.push_back(1);
v.push_back(2);
vm.push_back(v);
v.clear();
v.push_back(3);
v.push_back(4);
vm.push_back(v);
v.clear();
v.push_back(5);
v.push_back(6);
vm.push_back(v);
1 2
3 4
5 6
我想获得像这样的整数向量的向量
1 3 5
2 4 6
I am looking for an elegant way to pivot a vector of vector prefarably using STL algorithms or boost
Sample data looks like this
vector<vector<int> > vm;
vector<int> v;
v.push_back(1);
v.push_back(2);
vm.push_back(v);
v.clear();
v.push_back(3);
v.push_back(4);
vm.push_back(v);
v.clear();
v.push_back(5);
v.push_back(6);
vm.push_back(v);
1 2
3 4
5 6
I want to get a vector of vectors of ints like this
1 3 5
2 4 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想最简单的解决方案就是编写一个简单的转置函数,其中包含两个循环:
按值返回应该很容易被优化掉。我不认为使用任何标准函数会变得更简单或更有效,但我可能是错的。
另一种方法是将所有数据存储在一个平面
向量
中,然后使用i*row_length + j
计算元素i, j
的位置> 或类似的东西。这样,转置不涉及数据复制,而只是改变索引的计算。I guess the simplest solution is just to write a simple
transpose
function with two loops in it:The return-by-value should be optimized away easily enough. I don't think it will get any simpler or more efficient by using any standard function, but I could be wrong.
An alternative approach would be to store all the data in one flat
vector
and then calculate the position of elementi, j
usingi*row_length + j
or something like that. This way, transposing involves no copying of data, but simply changing the calculation of the indices.看看 Boost MultiArray。您可以创建子视图您的数据。还有 vnl_matrix 具有转置方法。
Look at Boost MultiArray. You can create sub-views of your data. There's also the vnl_matrix which has a transpose method.
我将介绍一个转换行 -> 的包装器山口和山口 ->排。这将避免您复制所有数据。
典型用法是:
I would introduce a wrapper that converts row -> col and col -> row. This will prevent you having to copy all the data.
Typical usage would be:
如果您要使用 C++ 进行大量线性代数计算,您应该查看 Boost.uBlas。
If you're going to be doing lots of linear algebra in C++, you should check out Boost.uBlas.
它绝对必须是向量的向量吗?
如果没有,那么您可能可以在行*列元素的常规向量上实现包装器。
类似于:
并在 main 中:
编辑:
Vector 类将表示行或列数据。
它需要线性化矩阵、矩阵几何形状(行数和列数)以及它代表的行/列作为参数。
用铅笔和纸试试这个:
1. 将 3x4 矩阵的元素写成矩阵形式
11 12 13 14
21 22 23 24
31 32 33 34
11 12 13 14 21 22 23 24 31 32 33 34
矩阵形式的元素索引与其线性化形式的索引之间存在简单的关系,该关系取决于矩阵的几何形状。
假设您的 Vector 需要引用矩阵中的第二列(即 12 22 32)。
然后:
- 向量中的第一项(即 Vector[1])是线性化矩阵中的第二个元素
- 向量中的第二项 22 是基础向量中的第 6 个元素
- 向量中的第三项 32 是 v_ 的第 10 个元素
现在,用铅笔和纸对矩阵的第三列(元素 13 23 33)执行相同的操作。
查看第二列的基础向量中的索引(分别为 2、6 和 10)与第三列的索引之间是否存在相似性。
Does it absolutely have to be a vector of vectors?
If not, then you could probably implement a wrapper over a regular vector of rows*columns elements.
Something like:
and in main:
EDIT:
The Vector class will represent the row or column data.
It needs, as parameters, the linearized matrix, the matrix geometry(number of rows and columns) and what row/column it represents.
Try this with pencil and paper:
1. write the elements of a 3x4 matrix in matrix form
11 12 13 14
21 22 23 24
31 32 33 34
11 12 13 14 21 22 23 24 31 32 33 34
There is a simple relation between an element's indices from the matrix form and its index in the linearized form which depends on the matrix geometry.
Say your Vector needs to reference the second column from the matrix (i.e. 12 22 32).
Then:
- the first item in the vector (i.e. Vector[1]) is the second element in the linearized matrix
- the second item in the vector, 22, is the 6th element from the underlying vector
- the third item in the vector, 32, is the 10th element of v_
Now, with pencil and paper do the same for the third column of the matrix( elements 13 23 33).
See if you spot any similarity between the indexes in the underlying vector for the second column (which were 2, 6 and 10) and the indexes you find for the third column.