Question

I'm working with Armadillo library in C++ and have a matrix-A. How do I acquire a 0/1 matrix-B where B_{ij} = 1 iff. A_{ij}>0?

I really can't afford writing a for loop in my program 'cause it's inside four layers of such.

Thank you!!

Was it helpful?

Solution

This is accomplished via Armadillo's relational operators:

 mat A = randu<mat>(5,5) - 0.5;

umat B = (A > 0);

Note that B has the type "umat" instead of "mat". The former contains unsigned integer elements.

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