// Write a program to calculate the innerproduct of two arrays (ip of v,w = sum, for all i, of vi*wi)
var v = [1, 2, 3, 'x'];
var w = [1, 2, 3, 4];
vSum_total = 0;
wSum_total = 0;
// calculate sum of v
for(i=0; i<v.length; i++)
{
if (isNaN(v[i]))
v[i] = 0
else
vSum_total += Number(v[i]);
}
// calculate sum of w
for(i=0; i<v.length; i++)
{
if (isNaN(v[i]))
v[i] = 0
else
wSum_total += Number(w[i]);
}
alert ("Total of v: " + (vSum_total));
alert ("Total of w: " + (wSum_total));
alert ("Inner product: " + (vSum_total * wSum_total));
我試圖計算一個數組的內積,但是我的結果不是提示。任何提示正確的方向將非常感激!計算陣列的內積
愚蠢的問題:是'vSum_total'初始化爲值'1'? –
順便說一句,計算'內積'需要兩個向量,而不是一個 –
什麼是'vSum'?你的意思只是'v' –