Is it possible to make a (sparse) matrix with the C++ Eigen library similar to this elegant python code I need to translate?
(np.random.rand(100,100) < 0.1) * np.random.rand(100,100)
e.g. a matrix filled with a certain proportion of random values.
davidhigh's answer addresses the sparse requirement of your question. However, I don't think that your python code actually produces a sparse matrix, but rather a dense matrix with mostly zeros. A similarly elegant version for Eigen can be
Note that this uses the standard C++
rand()
, so may not be sufficiently "random", depending on your needs. You can also replaceMatrixXd
withMatrixXf
if you preferfloat
s overdouble
s (change thecast<...>()
as well).