开发者

Rotating Matrix

I have a matrixA like that

[0][1]
[2][3]
[4][5]

And after my custom rotation (different numbering) becomes matrixB like this:

[0][1][2]
[3][4][5]

What i want is to map the numberings of the matrix A to B when i look the matrixA from the left->right. To explain: matrixA looking from the left->right looks like this.

[1][3][5]
[0][2][4]

And matrixB is as it is

[0][1][2]
[3][4][5]

So i want to map, preferably with an equation these values

1->0
3->1
5->2
0->3
2->4
4->5

The real matrix is much large开发者_运维知识库r so please don't focus to the size of this matrix

If anyone has any suggestions to find an equation for this mappings or some other way to do the mapping described? i would appreciate it


Here is some code I use sometimes. This rotates the matrix by 90 or -90 degrees. This might be a start for your problem:

public int[][] rotateMatrixRight(int[][] matrix)
{
    /* W and H are already swapped */
    int w = matrix.length;
    int h = matrix[0].length;
    int[][] ret = new int[h][w];
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            ret[i][j] = matrix[w - j - 1][i];
        }
    }
    return ret;
}


public int[][] rotateMatrixLeft(int[][] matrix)
{
    /* W and H are already swapped */
    int w = matrix.length;
    int h = matrix[0].length;   
    int[][] ret = new int[h][w];
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            ret[i][j] = matrix[j][h - i - 1];
        }
    }
    return ret;
}


Here you can find ten different formulas for your sequence 3,0,4,1,5,2.

Always consult OEIS when you need an integer sequence!


int height = 2, width = 3;  // of matrix B
int [][] a = {{0,1}, {2, 3}, {4, 5}};
int [][] b = {{0,1,2}, {3,4,5}};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
        int b_val = b[i][j];
        int a_val = a[j][height - i - 1];
        map.put(a_val, b_val);
    }
}

Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
Iterator<Map.Entry<Integer, Integer>> it = entries.iterator();
while(it.hasNext()) {
    Map.Entry<Integer, Integer> e = it.next();
    System.out.println(e.getKey() + " -> " + e.getValue());
}

See here in action.


Maybe something like this:

map(matrixA,x,y)
  w=width of matrix A
  h=height of matrix A
  n=y*w+x
  from=matrixA(x,y)
  to=matrixA(n mod h, n / h)
  return (from, to)

To make a map, just iterate over all x and y and make a mapping of all those variables.


in place C solution follows

void rotateRight(int matrix[][SIZE], int length) {

    int layer = 0;

    for (int layer = 0; layer < length / 2; ++layer) {

        int first = layer;
        int last = length - 1 - layer;

        for (int i = first; i < last; ++i) {

            int topline = matrix[first][i];
            int rightcol = matrix[i][last];
            int bottomline = matrix[last][length - layer - 1 - i];
            int leftcol = matrix[length - layer - 1 - i][first];

            matrix[first][i] = leftcol;
            matrix[i][last] = topline;
            matrix[last][length - layer - 1 - i] = rightcol;
            matrix[length - layer - 1 - i][first] = bottomline;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜