1
我已經float類型兩個向量(這裏簡化,我使用的3項矢量的長度):LINQ:結合矢量
float[] a = new float[] { 10.0f, 20.0f, 30.0f };
float[] b = new float[] { 5.0f, 10.0f, 20.0f };
我需要在成對的方式從一個。減去B和保持兩個向量(結果和剩餘數量)。結果相應於AB和剩餘量對應於不能從一箇中減去該量(參見實施例2):
實施例1:
a | b | Result | Remaining quantity (quantity that cannot be substracted from a)
(b remainder)
------------------------------------------------
10.0f 5.0f 5.0f 0f
20.0f 10.0f 10.0f 0f
30.0f 20.0f 10.0f 0f
在上面的例子中,剩餘量始終爲0,因爲B的數量總是小於或等於a的數量,但是,如果b中的任何數量大於a中的數量,則最多隻能減少a的數量,然後剩餘的數量應該是b不可能減少的餘數例如,想象下面的情況:
例2:
float[] a = new float[] { 10.0f, 5.0f, 30.0f };
float[] b = new float[] { 5.0f, 10.0f, 20.0f };
a | b | Result | Remaining (quantity that cannot be substracted from a)
(b remainder)
--------------------------------
10.0f 5.0f 5.0f 0f
5.0f 10.0f 0.0f 5f
30.0f 20.0f 10.0f 0f
操作後,我需要在兩個向量中獲得Result和Remaining。怎麼可以有效地使用LINQ和Zip之類的函數呢?
在此先感謝!
編輯: 我努力做到以下幾點:
float[] remainder = new float[<same lenght of a or b>];
Result= a
.Zip(b, (x, y) =>
{
remainder[i] = 0;
if (y > x)
{
remainder[i] = y - x;
return 0;
}
else
{
return x- y;
}
}).ToArray();
我現在的問題是要知道如何得到當前迭代的索引,在我的情況下,這個「我」被使用,但如何做到這一點?
是的,'Zip'是要走的路。 –
你知道如何得到當前迭代索引在zip中嗎? – user1624552
您可以Zip,然後選擇[this](http://msdn.microsoft.com/zh-cn/library/bb534869.aspx)。 –