Search code examples
c#arraysilnumerics

How to clamp array data in ILNumerics


I want to be able to set all the values in an ILArray<T> that are outside of a Min/Max value range to a Min/Max value.

Is there an ILNumerics array method that will do this?

for example in my 1000,1000 range array values range from 1 to 2000 but most of the data is in the range of 800-1000 so I want to set any value lower than 800 to 800 and any value higher than 1000 to 1000.

Without going through a nested for loop and checking every value is there a simpler way to do this?


Solution

  • There is no such method in ILNumerics, which clamps the values of an array to certain limits. However, as evertqin showed, you can do the max and min range separately. In order to clamp the values of an array ranging from 1 ..4 to 2..3 you could do:

    ILArray<double> data = new[,] {{1.0, 2.0}, {3.0, 4.0}};
    data[data > 3] = 3;
    data[data < 2] = 2;