Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How to offset matrix values from up to down by row in Javasript?

I have a matrix and a flip_value that goes for the first element, I want to offset or push down the numbers of a selected column. For example

let matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16],

];

I want to change the matrix-like that: So in the second (1st row in the program) is pushed down by 1 and the first element changed.

flip_value  = 999
let matrix =  [1, 999, 3, 4],
        [5, 2, 7, 8],
        [9, 6, 11, 12],
        [13, 10, 15, 16],

The new flip value is 14.

Could you also give me a hint on how to do this from down to up? flip_value = 999 Like that:

let matrix = [
    [1, 6, 3, 4],
    [5, 10, 7, 8],
    [9, 14, 11, 12],
    [13, 999, 15, 16],

];

//In this method the second-row values are pushed down to up by 1 and the last value in second row the last element flipped. The new flip_value = 2 here

The whole code which is not working

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] + " ");
        }

    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

From down to up or from up to down are pretty similar, speaking of the logic. There are just three things that changes between them:

Change\Type of push Up-down Down-up
flip_tmp (1) matrix[0][Yindex] matrix[size - 1][Yindex]
for-loop (2) for(i = 1; i < size; i++) for(i = size - 1; i > 0; i--)
matrix's flip value (3) matrix[size - 1][Yindex] matrix[0][Yindex]

With this said, you can adapt your code to handle either case by passing it as a string, for example.

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)

about 4 years ago · Juan Pablo Isaza Report

0

Abstraction is a useful idea here. First, the matrix can abstracted as object with accessors that hide the internal representation. The accessors the OP needs are get/set column.

An array that rotates and keeps state is another tool that solves this problem (and maybe others).

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();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!