2013-10-01 32 views
2

我有一個ilnumerics邏輯對稱矩陣那樣如何在ilnumerics邏輯對稱矩陣中查找維度?

0 0 0 0 0 0 1 1 1 1 
0 0 0 0 0 1 0 1 1 1 
0 0 0 0 0 1 1 0 1 1 
0 0 0 0 0 1 1 0 0 1 
0 0 0 0 0 1 1 1 1 0 
0 1 1 1 1 0 0 0 0 0 
1 0 1 1 1 0 0 0 0 0 
1 1 0 0 1 0 0 0 0 0 
1 1 1 0 1 0 0 0 0 0 
1 1 1 1 0 0 0 0 0 0 

欲獲得所有維度(行&列),其中值== 1

結果= (0,6),(0, (2,5),(2,6),(0,8),(0,9) (1,5),(1,7),(1,8),(1,9) ,(2,8),(1,9) (3,5),(3,6),(3,9) (4,5),(4,6),(4,7),( 4,8)

有沒有更快捷的方法來做到這一點s在C#中使用ilnumerics庫嗎?

編輯:這裏是我的解決方案

ILNumerics.ILLogical matrixThreshold; 
    ..... Some C# code 
    for (int i = 0; i < matrixThreshold.Length; i++) 
     for (int j = i + 1; j < matrixThreshold.Length; j++) 
      if (matrixThreshold.GetValue(i, j) == 1) Console.Write("({0},{1}){2}", i, j, Environment.NewLine); 
+1

要小心ILArray .Length!它給出了最長維度長度 - 這不是你想要的那種情況。對於列長度,最好使用matrixThreshold.S [0],對於行長度使用.S [1]。 – user492238

回答

2
ILLogical L = ILMath.rand(10, 12) > 0.5; 

>L 
Logical [10,12] 
    [0]: 0 0 0 1 1 1 0 0 0 0 1 1 
    [1]: 0 1 1 1 0 1 1 1 1 0 1 0 
    [2]: 0 1 1 1 1 1 0 1 1 0 1 1 
    [3]: 0 0 0 0 0 1 0 0 0 0 1 0 
    [4]: 0 0 0 0 0 0 1 0 1 1 0 0 
    [5]: 0 0 1 0 0 1 0 0 1 1 1 1 
    [6]: 1 0 1 0 1 1 0 1 0 1 0 1 
    [7]: 1 1 1 0 1 1 0 1 1 1 0 1 
    [8]: 1 0 0 1 1 1 0 0 1 0 0 0 
    [9]: 1 1 1 1 0 0 0 0 1 1 0 0 

寫出行/列的索引成對:

ILArray<int> C = 1; 
ILArray<int> R = ILMath.find(L, 0, C); 
// C now holds the column indices, R the row indices 
for (int i = 0; i < C.Length; i++) { 
    System.Diagnostics.Debug.WriteLine("({0},{1})", R.GetValue(i), C.GetValue(i)); 
} 

其中給出:

(6,0) 
(7,0) 
(8,0) 
(9,0) 
(1,1) 
(2,1) 
(7,1) 
(9,1) 
(1,2) 
(2,2) 
(5,2) 
(6,2) 
(7,2) 
(9,2) 
(0,3) 
(1,3) 
(2,3) 
(8,3) 
(9,3) 
(0,4) 
(2,4) 
(6,4) 
(7,4) 
(8,4) 
(0,5) 
(1,5) 
(2,5) 
(3,5) 
(5,5) 
(6,5) 
(7,5) 
(8,5) 
(1,6) 
(4,6) 
(1,7) 
(2,7) 
(6,7) 
(7,7) 
(1,8) 
(2,8) 
(4,8) 
(5,8) 
(7,8) 
(8,8) 
(9,8) 
(4,9) 
(5,9) 
(6,9) 
(7,9) 
(9,9) 
(0,10) 
(1,10) 
(2,10) 
(3,10) 
(5,10) 
(0,11) 
(2,11) 
(5,11) 
(6,11) 
(7,11) 
+0

你可以簡單地用3個參數調用find()。這會簡化代碼。 –

+0

非常感謝。 **子公司問題:**我想按欄目總結,這是更快的方法嗎? 'ILNumerics.ILRetArray sumCol = ILNumerics.ILMath.sum(L); ' – LeMoussel

+0

絕對。但我建議指定明確的總和。它爲L是行向量的情況帶來更多的魯棒性。 –

-1
for(int i=0;i<num1;i++) 
    { 
    for j=0;j<num2;j++) 
    { 
    if(a[i][j]) 
     print("("+i+","+j+")"); 
     } 
    } 
+0

可否請您重新輸入您的代碼,以便它會編譯 – rhughes

+0

不存在ilnumerics原生函數? 例如R語言中的dist()函數。 – LeMoussel