Pedro77
Pedro77

Reputation: 5294

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

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².

Upvotes: 2

Views: 627

Answers (1)

Peter
Peter

Reputation: 14937

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

Upvotes: 2

Related Questions