From Matlab to C++ Eigen matrix operations - vector normalization

大憨熊 提交于 2019-12-08 02:16:44

问题


Converting some Matlab code to C++.

Questions (how to in C++):

  1. Concatenate two vectors in a matrix. (already found the solution)

  2. Normalize each array ("pts" col) dividing it by its 3rd value

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,:));

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);
}

Edit: I hope the question is more clear now².


回答1:


For #2:

C = C.array().rowwise() / C.row(2).array();

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



来源:https://stackoverflow.com/questions/42741074/from-matlab-to-c-eigen-matrix-operations-vector-normalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!