我有包含與x座標,y座標,和值的子陣列用於矩陣的陣列的有效方法:用於將線性陣列以矩陣
// [x,y,value]
var arr = [
[1,2,0.01],
[1,3,0.02],
[1,4,0.05],
[1,5,0.03],
[2,3,0.04],
[2,4,0.02],
[2,5,0.01],
[3,4,0.06],
[3,5,0.05],
[4,5,0.07],
]
然後,我有一個零填充的2D陣列( 「矩陣」)x_max×x_max維度。我試圖用一個計算有效的方法來填補這個矩陣的值如下:
// already have a variable called 'matrix' which is zero-filled
function constructMatrix(){
for(var i in arr){
var y = arr[i][0];
var x = arr[i][1];
var val = arr[i][2];
matrix[y][x] = val;
}
}
什麼,我得到的是具有獨特的列值的矩陣,但跨行相同的值。在我的邏輯中有沒有簡單的中斷?
我希望類似以下的輸出:
var matrix = [
[0.01,0.02,0.05,0.03],
[0,0.04,0.02,0.01],
[0,0,0.06,0.05],
[0,0,0,0.07],
]
您能給我們一個預期輸出的例子嗎? –
@kevinternet你在那裏。 – Wagtail
這是一個問題的小提琴:https://jsfiddle.net/ocwna691/ – Wagtail