0

This answer offers a single element solution:

#include <cmath>
#include <iostream>

#include <Eigen/Core>

double Exp(double x) // the functor we want to apply
{
    return std::exp(x);
}

int main()
{
    Eigen::MatrixXd m(2, 2);
    m << 0, 1, 2, 3;
    std::cout << m << std::endl << "becomes: ";
    std::cout << std::endl << m.unaryExpr(&Exp) << std::endl;
}

However, I want to apply a sliding window to a Matrix so I could write a function:

double foo(Eigen::Matrix2d x) // the functor we want to apply
{
    // Whatever
}

Is this possible?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • I don't think there is something already but looping over blocks should be really easy to implement by yourself. – Henri Menke Apr 26 '18 at 05:46
  • Are you asking how to write a function that can operate on a block of a matrix? – Avi Ginsburg Apr 26 '18 at 05:54
  • 2
    Your example is very vague, e.g., what output do you like to have. Could you write a nested loop (at least to explain what you want). And will your `foo` function have special properties, e.g., is it linear? – chtz Apr 26 '18 at 06:01
  • You will probably have to write such a thing yourself. Look for something shaped like `Matrix apply_window(Matrix, Func)`, but don't expect to find it – Caleth Apr 26 '18 at 08:47
  • @Caleth It really depends on what the OP is looking for. E.g., if `foo(x)=x[0]-x[1]`, then the solution is `m.topRows(m.rows()-1)-m.bottomRows(m.rows()-1)` – chtz Apr 26 '18 at 11:23

0 Answers0