简体   繁体   中英

Transform a 9x9 Matrix so that each row is a 3x3 Block

I have the following example data:

[0] = {01,02,03,  04,05,06,   07,08,09}
[1] = {11,12,13,  14,15,16,   17,18,19}
[2] = {21,22,23,  24,25,26,   27,28,29}
[3] = {31,32,33,  34,35,36,   37,38,39}
[4] = {41,42,43,  44,45,46,   47,48,49}
[5] = {51,52,53,  54,55,56,   57,58,59}
[6] = {61,62,63,  64,65,66,   67,68,69}
[7] = {71,72,73,  74,75,76,   77,78,79}
[8] = {81,82,83,  84,85,86,   87,88,89}

To create the new matrix I did this, Knowing it static 9x9

var grid2=[[],[]];
grid2.push([grid[0][0],grid[0][1], grid[0][2],grid[1][0],grid[1][1], grid[1][2],grid[2][0],grid[2][1], grid[2][2]]);
grid2.push([grid[3][0],grid[3][1], grid[3][2],grid[4][0],grid[4][1], grid[4][2],grid[5][0],grid[5][1], grid[5][2]]);
grid2.push([grid[6][0],grid[6][1], grid[6][2],grid[7][0],grid[7][1], grid[7][2],grid[8][0],grid[8][1], grid[8][2]]);
grid2.push([grid[0][3],grid[0][4], grid[0][5],grid[1][3],grid[1][4], grid[1][5],grid[2][3],grid[2][4], grid[2][5]]);
grid2.push([grid[3][3],grid[3][4], grid[3][5],grid[4][3],grid[4][4], grid[4][5],grid[5][3],grid[5][4], grid[5][5]]);
grid2.push([grid[6][3],grid[6][4], grid[6][5],grid[7][3],grid[7][4], grid[7][5],grid[8][3],grid[8][4], grid[8][5]]);
grid2.push([grid[0][6],grid[0][7], grid[0][8],grid[1][6],grid[1][7], grid[1][8],grid[2][6],grid[2][7], grid[2][8]]);
grid2.push([grid[3][6],grid[3][7], grid[3][8],grid[4][6],grid[4][7], grid[4][8],grid[5][6],grid[5][7], grid[5][8]]);
grid2.push([grid[6][6],grid[6][7], grid[6][8],grid[7][6],grid[7][7], grid[7][8],grid[2][6],grid[8][7], grid[8][8]]);

This works, but my question is, can this be done more efficiently / elegantly. The point of this, is part of a larger code base, that takes the matrix and validates if it is a Sudoku solution.

You can use a simple algorithm for this

let idx = [0, 1, 2], idy = [0, 1, 2];

let result = [];
for(let i=0; i<9; i+=3){
    for(let j=0; j<9; j+=3){
        idx.forEach(e => {
            idy.forEach(f => {
                result.push(arr[i+e][j+f]);
            })
        })
    }
}

You could take a nested approach for transforming the given 2D to a 4D array.

 var data = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [11, 12, 13, 14, 15, 16, 17, 18, 19], [21, 22, 23, 24, 25, 26, 27, 28, 29], [31, 32, 33, 34, 35, 36, 37, 38, 39], [41, 42, 43, 44, 45, 46, 47, 48, 49], [51, 52, 53, 54, 55, 56, 57, 58, 59], [61, 62, 63, 64, 65, 66, 67, 68, 69], [71, 72, 73, 74, 75, 76, 77, 78, 79], [81, 82, 83, 84, 85, 86, 87, 88, 89],], result = data.reduce((r, a, i) => ( a.forEach((b, j) => [Math.floor(i / 3), Math.floor(j / 3), i % 3].reduce( (s, k) => s[k] = s[k] || [], r )[j % 3] = b), r), []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM