将一些Matlab代码转换为C++。
问题(在C++中如何操作):
1和2的Matlab代码
% 1. A 3x1 vector. d0, d1 double.
B = [d0*A (d0+d1)*A]; % B is 3x2
% 2. Normalize a set of 3D points
% Divide each col by its 3rd value
% pts 3xN. C 3xN.
% If N = 1 you can do: C = pts./pts(3); if not:
C = bsxfun(@rdivide, pts, pts(3,:));
1和2的C++代码
// 1. Found the solution for that one!
B << d0*A, (d0 + d1)*A;
// 2.
for (int i=0, i<N; i++)
{
// Something like this, but Eigen may have a better solution that I don't know.
C.block<3,1>(0,i) = C.block<3,1>(0,i)/C(0,i);
}
编辑:
我希望这个问题现在更加清楚²。
最佳答案
对于#2:
C = C.array().rowwise() / C.row(2).array();
只有数组具有为行和列的部分约简定义的乘法和除法运算符。当您将其分配回
C
时,该数组将转换回矩阵关于c++ - 从Matlab到C++特征矩阵运算- vector 归一化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42741074/