1

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?

Abstract type
  • 1,901
  • 2
  • 15
  • 26
Jeff Davis
  • 1,755
  • 2
  • 11
  • 14

1 Answers1

1

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.

  • What I was looking for was a faster ILMath function could populate the Colors and Postions arrays with their values with a single statement. – Jeff Davis Feb 02 '16 at 18:59