从Matlab到C ++特征矩阵运算-向量归一化 [英] From Matlab to C++ Eigen matrix operations - vector normalization
本文介绍了从Matlab到C ++特征矩阵运算-向量归一化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将一些Matlab代码转换为C ++.
Converting some Matlab code to C++.
问题(在C ++中如何操作):
Questions (how to in C++):
-
将两个向量串联在一起. (已经找到了解决方案)
Concatenate two vectors in a matrix. (already found the solution)
归一化每个数组("pts" col)除以其第3个值
Normalize each array ("pts" col) dividing it by its 3rd value
1和2的Matlab代码
Matlab code for 1 and 2:
% 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 ++代码
C++ code for 1 and 2:
// 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);
}
我希望这个问题现在更加清楚².
I hope the question is more clear now².
推荐答案
对于#2:
C = C.array().rowwise() / C.row(2).array();
仅数组具有针对行和列部分约简定义的乘法和除法运算符.当您将其分配回C
Only arrays have multiplication and division operators defined for row and column partial reductions. The array converts back to a matrix when you assign it back into C
这篇关于从Matlab到C ++特征矩阵运算-向量归一化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文