Question

H, W, R,V are matrices and are already initialized with respective sizes. "beta" is an int, "myeps" is a float. For the matrix implementation I have currently used Eigen library. However I'm not sure of the syntax to convert this Matlab code successfully to Eigen based C++ code.

Matlab Code

H = H .* ( (W'*(R.^(beta-2) .* V)) ./ max(W'*R.^(beta-1), myeps) ); 

C++ Code (What I tried so far)

WH = W_ * H_;

Eigen::MatrixXf j=(W_.transpose().array()*(WH.array().pow((beta2)).cwiseProduct(V.array())));

Eigen::MatrixXf k=(W_.transpose().array()*((WH.array().pow(beta-1))));

float m=max(k.maxCoeff(),0.001);

H_ = H_.cwiseProduct(j/m);

Is this code correct?

FYI - This is a step in a NMF Algorithm(Non-negative matrix Factorization based on beta divergence).

Any help would be greatly appreciated.

Was it helpful?

Solution

This does not seem correct. * on arrays is equivalent to a cwiseProduct while you want a matrix product with W'. Proposition:

Eigen::MatrixXf j = (W.transpose() * (R.array().pow(beta-2)*V.array()).matrix());
Eigen::MatrixXf k = (W.transpose() *  R.array().pow(beta-1).matrix());
H = H.cwiseProduct(j/max(k.maxCoeff(),myeps));

OTHER TIPS

I recommend you have a look at this quick reference:

http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt

It contains mappings between Eigen and Matlab.

For example:

R = P.cwiseProduct(Q);    // R = P .* Q

and

R.array().square()        // P .^ 2

As you can see, this Rosetta Stone can help you translate your expressions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top