Tengo una matriz y un flip_value que corresponde al primer elemento, quiero compensar o reducir los números de una columna seleccionada. Por ejemplo
let matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], ];Quiero cambiar la matriz así: Entonces, en la segunda (primera fila en el programa) se empuja hacia abajo 1 y el primer elemento cambió.
flip_value = 999 let matrix = [1, 999, 3, 4], [5, 2, 7, 8], [9, 6, 11, 12], [13, 10, 15, 16],El nuevo valor de volteo es 14.
¿Podría también darme una pista sobre cómo hacer esto de abajo hacia arriba? flip_value = 999 Así:
let matrix = [ [1, 6, 3, 4], [5, 10, 7, 8], [9, 14, 11, 12], [13, 999, 15, 16], ];//En este método, los valores de la segunda fila se empujan hacia abajo y hacia arriba en 1 y el último valor en la segunda fila es el último elemento invertido. El nuevo flip_value = 2 aquí
Todo el código que no funciona.
let matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], ]; let Yindex = 1; let flip_value = 999; let size = 4; let flip_tmp = matrix[size - 1][Yindex]; console.log("fontos"); for (let i = 1; i < size; i++) { // matrix[i][Yindex] = matrix[i-1][Yindex]; let tmp = matrix[i][Yindex]; matrix[i][Yindex] = matrix[i - 1][Yindex]; matrix[i - 1][Yindex] = tmp; console.log(matrix[i][Yindex] + "=" + matrix[i - 1][Yindex]); } matrix[0][Yindex] = flip_value; flip_value = flip_tmp; for (let i = 0; i < size; i++) { console.log("\n"); for (let j = 0; j < size; j++) { console.log(matrix[i][j] + " "); } } for (let i = 0; i < size; i++) { console.log("\n"); for (let j = 0; j < size; j++) { if (j == 1) { console.log(i + "" + j + " " + matrix[i][j] + " "); } } }De abajo hacia arriba o de arriba hacia abajo son bastante similares, hablando de la lógica. Solo hay tres cosas que cambian entre ellos:
| Cambiar\Tipo de empuje | Arriba abajo | abajo arriba |
|---|---|---|
| flip_tmp (1) | matrix[0][Yindex] | matrix[size - 1][Yindex] |
| bucle for (2) | for(i = 1; i < size; i++) | for(i = size - 1; i > 0; i--) |
| valor de inversión de la matriz (3) | matrix[size - 1][Yindex] | matrix[0][Yindex] |
Dicho esto, puede adaptar su código para manejar cualquier caso pasándolo como una cadena, por ejemplo.
let pushDirection = "up-down"; // or down-up if not specified let Yindex = 1; let flip_value = 999; let flip_tmp; function swap(matrix, i, j){ let tmp = matrix[i][j]; matrix[i][j] = matrix[i - 1][j]; matrix[i - 1][j] = tmp; } let matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], ]; let size = matrix.length if(pushDirection === "up-down"){ flip_tmp = matrix[0][Yindex]; // 1 for(let i = 1; i < size; i++) { // 2 swap(matrix, i, Yindex); } matrix[size - 1][Yindex] = flip_value; // 3 } else { // down-up flip_tmp = matrix[size - 1][Yindex]; // 1 for(let i = size-1; i > 0; i--) { // 2 swap(matrix, i, Yindex); } matrix[0][Yindex] = flip_value; // 3 } flip_value = flip_tmp; console.log("Push direction:", pushDirection, "\n", JSON.stringify(matrix).replaceAll('],','],\n')) console.log("New flip value: ", flip_value)La abstracción es una idea útil aquí. Primero, la matriz puede abstraerse como objeto con accesores que ocultan la representación interna. Los accesores que necesita el OP son la columna get/set.
Una matriz que gira y mantiene el estado es otra herramienta que resuelve este problema (y quizás otros).
class Matrix { constructor(arrayOfArrays) { this.array = arrayOfArrays } getColumn(j) { // out of bounds check for the reader return this.array.map(row => row[j]) } setColumn = (j, colValues) => { // array length and out of bounds check for the reader this.array.forEach((row, i) => row[j] = colValues[i]) } // just for illustration.. more like this left to the reader getRow(i) { return this.array[i] } print() { this.array.forEach(row => console.log(JSON.stringify(row))) } } // array that rotates and keeps state about the last value popped class RotatingArray { constructor(array) { this.array = array this.rotateValue = null } rotateForward(value) { value = value || this.rotateValue this.rotateValue = this.array[this.array.length-1] this.array = [value, ...this.array.slice(0, -1)]; return this.array } rotateReverse(value) { value = value || this.rotateValue this.rotateValue = this.array[0] this.array = [...this.array.slice(1), value]; return this.array } } // those are the tools needed to solve the problem // testing, rotate the 1st col forward with 999, do it twice let data = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], ]; let matrix = new Matrix(data) let firstCol = matrix.getColumn(1); let rArr = new RotatingArray(firstCol); firstCol = rArr.rotateForward(999) matrix.setColumn(1, firstCol); matrix.print(); firstCol = rArr.rotateForward() matrix.setColumn(1, firstCol); console.log('after a second rotation') matrix.print();