Search code examples
c#ilnumerics

ILMath matrix loading


I am just getting started with ILNumerics. I'm not that familiar with all the ILMath matrix array functions.

I created a custom color map that I am using with a ILSurface plot and am manually converting it into an array for use in the ILColormap() creation.

ColorBlend colorblend new ColorBlend // my color map
{
    Positions = new[] {0, 0.40F, 0.55F, 0.90F, 1},
    Colors = new[] {Color.Blue, Color.Lime, Color.Yellow, Color.Red, Color.FromArgb(255, 102, 102)}
},


ILArray<float> data = ILMath.zeros<float>(colorBlend.Colors.Length,5);
for (var i = 0; i < data.Length; i++)
{
    data[i, 0] = colorBlend.Positions[i];
    data[i, 1] = colorBlend.Colors[i].R / 255f;
    data[i, 2] = colorBlend.Colors[i].G / 255f;
    data[i, 3] = colorBlend.Colors[i].B / 255f;
    data[i, 4] = colorBlend.Colors[i].A / 255f;
}

Isn't there an easier way than the for loop to build this array?


Solution

  • What is wrong with your code? One could use plain Linq to prevent from the loop:

    data.a = ILMath.reshape<float>(colorBlend.Positions.SelectMany(
     (f, i) => new[] {
                      f,
                      colorBlend.Colors[i].R / 255f,
                      colorBlend.Colors[i].G / 255f,
                      colorBlend.Colors[i].B / 255f,
                      colorBlend.Colors[i].A / 255f
                      },
    (f, c) => c).ToArray(), 5, colorBlend.Positions.Length).T;
    

    But personally, I don’t think it is worth the effort. I like your version best.