matrixvectoradditionarrayfire

Add a vector to all columns of a matrix with ArrayFire


What is the best way to add a vector to all columns of a Matrix with ArrayFire?

At the moment i'm using a gfor loop, but that seems wrong for such a simple task.

gfor(af::seq i, M.dims(1)) {
    M(af::span, i) += VECTOR;
}

Is there a better way?


Solution

  • You can use tile. Since you are tiling a singleton dimension (VECTOR.dims(1) = 1), this will be done as a JIT operation (in the same kernel) and not call a different kernel.

    M += af::tile(VECTOR, 1, M.dims(1));