1

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?

nik
  • 1,672
  • 2
  • 17
  • 36

2 Answers2

3

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

user492238
  • 4,094
  • 1
  • 20
  • 26
1

The most efficient way is not the easiest to maintain. Do a single check for the operator and repeat the for loop for each different operator.

zam664
  • 757
  • 1
  • 7
  • 18