'Diagonal matrix properly in armadillo

My code works but I'm just curious to see if someone knows how to do this but properly using Armadillo library. Thanks for your time :)

arma::mat W = arma::mat(4, 4, arma::fill::ones);
arma::mat D = arma::mat(4, 4, arma::fill::zeros);

for (size_t i = 0; i < W.n_rows; i++)
{
    for (size_t j = 0; j < W.n_cols; j++)
    {
        D(i, i) += W(i, j);
    }
}
std::cout<< "W = \n"<< W <<std::endl;
std::cout<< "D = \n"<< D <<std::endl;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source