My code involves an array that contains subarrays with x-coordinates, y-coordinates, and values representing a matrix:
// [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],
]
I also have a 2D array ("matrix") of dimensions x_max X x_max filled with zeroes. I'm attempting to efficiently populate this matrix using the following method:
// assuming 'matrix' is already defined and 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;
}
}
However, my resulting matrix has unique column values but duplicate values across rows. Can you help me identify where my logic might be flawed?
The expected output should resemble the following:
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],
]