Search code examples
c#matlabilnumerics

bsxfun type function in c#


I m trying to do the equivalent of the following matlab function:

outmatrix = bsxfun(@minus,inmatrix, invector);

in c sharp. I used this:

public static ILArray<double> bsxfun(ILArray<double> inmatrix, ILArray<double> invector)
    {

        for(int i=0; i < inmatrix.getlength(1) ;i++)
        {
            inmatrix[":",i] = inmatrix[":",i] -invector;
        }
        return inmatrix;

    }

Utilizing ILNumerics package.

My questions: is this the most efficient way? because my matrices can be large. How can I generalize this so that I can specify whether to do minus, plus, times, divide, etc like with a funciton handle?


Solution

  • In ILnumerics you dont need to do anything. ILNumerics automatically operates the vector on the matrix elements correctly:

     outmatrix = inmatrix - invector; 
    

    Docu: http://ilnumerics.net/Opoverload.html

    BTW: if you want efficient implementation you must use the ILNumerics Function rules: http://ilnumerics.net/FunctionRules.html