Add matlab's repelem equivalent
@ggael
Submitted by Gael GuennebaudAssigned to Nobody
Link to original bugzilla bug (#1778)
Version: 3.4 (development)
Description
This is a frequently asked feature. Thanks to IndexedView, we can easily implement it as:
template<typename XprType, typename RowFactorType, typename ColFactorType>
auto repelem(const XprType &xpr, RowFactorType row_factor, ColFactorType col_factor) {
using namespace Eigen;
const int RowFactor = internal::get_fixed_value<RowFactorType>::value;
const int ColFactor = internal::get_fixed_value<ColFactorType>::value;
const int NRows = XprType::RowsAtCompileTime == Dynamic || RowFactor == Dynamic ? Dynamic : XprType::RowsAtCompileTime*RowFactor;
const int NCols = XprType::ColsAtCompileTime == Dynamic || ColFactor == Dynamic ? Dynamic : XprType::ColsAtCompileTime*ColFactor;
const int nrows = internal::get_runtime_value(row_factor) * xpr.rows();
const int ncols = internal::get_runtime_value(col_factor) * xpr.cols();
return xpr(
Array<int,NRows,1>::LinSpaced(nrows,0,xpr.rows()-1),
Array<int,NCols,1>::LinSpaced(ncols,0,xpr.cols()-1)
);
}
Full demo: https://godbolt.org/z/rYgDxF
This can easily be turned as a member function and specialized for Horizontal or Vertical replication only.
What would be a good name given that we already have "replicate" as an equivalent to "repmat" ?
Can we do better than the above implementation if using a dedicated expression (as for replicate)?
Edited by Eigen Bugzilla