2016-01-23 33 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數組中。 –