Kabu
Kabu

Reputation: 569

Eigen how to redistribute matrix values between zero and one

I have an Eigen matrix which is made of 3d vector coords, like this:

 Eigen::MatrixXd V(m,n);

 V <<
 0.07790318440941 ,  2.93084729477845 ,0
 4.39385211199504 , -6.85288411884976 ,0
 4.79942053354374 , -6.21531436940366 ,0
 4.66539716949595 , -5.80851415050593 ,0
 4.05673773136193 , -5.07476450520047 ,0
 3.86587436704604 , -5.95679712917311 ,0
 3.08037179920737 , -5.63304123859979 ,0
 3.63881426545377 , -6.81338736661468 ,0
 2.78034109511472 , -5.31824526094401 ,0
 2.49863667468366 , -4.42841643782729 ,0
 2.17676933907024 , -4.07462683865738 ,0
 1.83413618944877 , -3.26212572878334 ,0
 1.07129357571697 , -2.18125695931175 ,0
 0.87907607105418 , -1.47159374716746 ,0
 0.08567653234750 , -1.08987456825602 ,0
 1.05117121796426 , -0.398720814302181,0
 1.66426149221552 , -1.75532754482808 ,0
 2.41664333430651 , -2.85689387781641 ,0
 2.98468613101269 , -3.5024571681322  ,0
 3.25323005684431 , -3.9571858297574  ,0
 3.83255698099666 , -4.64451314176494 ,0
 3.1964644093435  , -4.78861644450358 ,0
 2.4829670255391  , -3.63635780062414 ,0
 1.70284397625327 , -2.49963796336543 ,0

(here the 3rd coord is missing) I want them to span values between 0 and 1 only, where 0 is min, and 1 max. In short, scale them. How can I do in Eigen?

Upvotes: 2

Views: 534

Answers (1)

dfrib
dfrib

Reputation: 73186

If you plan on apply your own normalization scheme based on the values of your dynamically sized matrix at a given time, you might want to consider using a fixed-sized const matrix instead, otherwise you would be scaling your matrix entries differently depending on how the dynamic entries of the matrix looks at a given time. Alternatively, apply your normalization based on what you consider the static min/max value of a given coordinate.

Anyway, if you want to go ahead with this with your current matrix, have a look at the minCoeff() and maxCoeff() functions.

Compute min and max (over all matrix entries), subtract min from all matrix entries (shift), and thereafter divide all matrix entries by max - min (scale). The shift and scale linear operations will result in a matrix where the minimum entry is valued 0 and the maximum is valued 1, and where all remaining values are linearly scaled accordingly, residing in [0, 1].

Upvotes: 1

Related Questions