2016-01-23 34 views
1

我刚刚开始使用ILNumerics。我不熟悉所有的ILMath矩阵数组函数。ILMath矩阵加载

我创建了一个自定义颜色映射,我正在使用ILSurface图并将其手动转换为用于创建ILColormap()的数组。

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; 
} 

是不是有一个比for循环更容易的方法来构建这个数组?

回答

1

你的代码有什么问题?人们可以使用简单的Linq来防止循环:

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; 

但个人而言,我不认为这是值得的努力。我最喜欢你的版本。

+0

我在找的是一个更快的ILMath函数,它可以用一条语句将它们的值填充到Colors和Postions数组中。 –