Rotating non square matrix in javascript

huangapple go评论69阅读模式
英文:

Rotating non square matrix in javascript

问题

我试图找到一种将矩形矩阵旋转90度的方法。

例如:

将这个旋转:

Rotating non square matrix in javascript

变成这个:

Rotating non square matrix in javascript

我有一个像这样的索引数组:

array = [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0]
width = 5
height = 3

这是我想要的:

array90 = [0,0,0,0,1,0,0,1,0,0,1,0,0,0,0]

谢谢。

英文:

Im trying to find a way to transform a rectangular matrix by 90 degree.

E.g:

rotate this:

Rotating non square matrix in javascript

to this:

Rotating non square matrix in javascript

I got an array of indices like that:

array = [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0];
width = 5;
height = 3;

Thats what I want to get:

array90 = [0,0,0,0,1,0,0,1,0,0,1,0,0,0,0];

Thanks in regard

答案1

得分: 0

在JavaScript中,要将一个矩形矩阵顺时针旋转90度,可以采用以下方法:

首先,通过交换矩阵的行和列来进行矩阵的转置操作。然后,反转行的顺序以获得旋转后的矩阵。

以下是这个方法的实现和一个示例:

// 这个函数以矩形矩阵作为输入,返回旋转后的矩阵
function rotateMatrix(matrix) {
  const rows = matrix.length;
  const cols = matrix[0].length;

  // 转置矩阵
  const transposedMatrix = [];
  for (let j = 0; j < cols; j++) {
    const row = [];
    for (let i = 0; i < rows; i++) {
      row.push(matrix[i][j]);
    }
    transposedMatrix.push(row);
  }

  // 反转行
  for (let i = 0; i < cols; i++) {
    transposedMatrix[i].reverse();
  }

  return transposedMatrix;
}

// 这个示例将一个3x3矩阵顺时针旋转90度。输出是旋转后的矩阵,其中行的顺序已反转。

const matrix = [
  [0, 0, 0, 0, 0],
  [0, 1, 1, 1, 0],
  [0, 0, 0, 0, 0],
];

const rotated = rotateMatrix(matrix);

console.log(rotated);

如果您遇到任何问题,请告诉我。

英文:

To transform a rectangular matrix by 90 degrees in JavaScript, you can use the following approach:

First, transpose the matrix by swapping its rows and columns. Then, reverse the order of the rows to obtain the 90-degree rotated matrix.

Here's an implementation of this approach and an example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

   //This function takes a rectangular matrix as its input and returns the rotated matrix
             function rotateMatrix(matrix) {
          const rows = matrix.length;
          const cols = matrix[0].length;

          // Transpose the matrix
          const transposedMatrix = [];
          for (let j = 0; j &lt; cols; j++) {
            const row = [];
            for (let i = 0; i &lt; rows; i++) {
              row.push(matrix[i][j]);
            }
            transposedMatrix.push(row);
          }

          // Reverse the rows
          for (let i = 0; i &lt; cols; i++) {
            transposedMatrix[i].reverse();
          }

          return transposedMatrix;
        }

            //This example rotates a 3x3 matrix by 90 degrees. The output is the rotated matrix, where the rows are in reverse order.

                const matrix = [
                  [0,0,0,0,0],
                  [0,1,1,1,0],
                  [0,0,0,0,0],

                ];

                const rotated = rotateMatrix(matrix);

                console.log(rotated); 

<!-- end snippet -->

Tell me if you are facing any problems.

huangapple
  • 本文由 发表于 2023年2月23日 21:02:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定