我尝试在C++中连接二维向量,就像python一样:
np.concatenate([x,y,z], axis=1)我尝试了下面的代码,但它沿行连接。
std::vector<std::vector<int>> dest{{1,2,3,4,5}};
std::vector<std::vector<int>> src{{6,7,8,9,10}};
dest.insert(
dest.end(),
src.begin(),
src.end()
);输出:
1 2 3 4 5 6 7 8 9 10但我希望它像下面这样:
1 6
2 7
3 8
4 9
5 10有没有办法像上面的python np.concatenate函数那样沿着列进行连接?我正在尝试可视化数据,所以我需要转置所有的向量并沿着列连接。
发布于 2019-01-30 23:20:44
您可以使用newmat11来实现所描述的与phyton类似的行为:http://www.robertnz.net/nm11.htm
std::vector<int > dest{ 1,2,3,4,5 };
std::vector<int > src{ 6,7,8,9,10 };
std::vector<int> third{ 11,12,13,14,15 };
Matrix x(5, 3);
x.column(1) << dest.data();
x.column(2) << src.data();
x.column(3) << third.data();
Matrix sub = x.submatrix(1, 5, 1, 2);
std::cout << sub << std::endl;生成以下输出
1.0 6.0
2.0 7.0
3.0 8.0
4.0 9.0
5.0 10.0Matrix连续存储它的值,因此您可以像这样获得单个向量:
std::vector<double> merge(sub.Store(), sub.Store() + sub.size());
for (auto& digit : merge) std::cout << digit << "\t";这将产生以下输出:1 6 2 7 3 8 4 9 5 10
“最难”的事情是:
https://stackoverflow.com/questions/54443657
复制相似问题