Problems with transpose a 2D array or matrix ...

Hello !

I have a question about the transpose of a 2D Array or Matrix. I wrote a short programm to explain my problem:

int [][] matrix = { {0, 0, 0}, 
                    {0, 1, 0}, 
                    {1, 1, 1} };

int [][] tmpMatrix = new int [3][3];
int [][] newMatrix = new int [3][3];

void setup() {
  size(800, 600);
  background(0);
  tmpMatrix = matrix;
}

void draw() {
  drawMatrix(newMatrix);
  for (int j = 0; j<3; j++) {
    for (int i = 0; i<3; i++) {

      **newMatrix[j][i]**=tmpMatrix[i][j];
    }

    print("\n");
  }
  drawMatrix(matrix);
  drawMatrix(tmpMatrix);
  drawMatrix(newMatrix);
  noLoop();
}

void drawMatrix(int [][] matrix) { 
  for (int j = 0; j<3; j++) {
    print("\n");
    for (int i = 0; i<3; i++) {
      print(matrix[j][i]);
    }
  }
  print("\n");
}

So this programm works fine and I get the transpose of the Matrix in "newMatrix".

But when I change the variable from "newMatrix" into "matrix" (I mean inside of the two for loops) the transposed matrix is wrong !

I don't understand what is the problem here.

Thank you a lot !

Answers

  • Answer ✓

    tmpMatrix = matrix;

    doesn't do what you think it does. this creates a reference between the two things, so now tmpMatrix isn't a copy of matrix, it's the exact same thing

    int[] a = {1};
    int[] b = {2};
    
    b = a; // not actually a copy
    
    // should print 1, looks like a copy...
    println(b);
    
    // but changing b also changes a
    b[0] = 3;
    println(a); // 3
    
  • edited January 2018

    Thanks ! That is what I thought too, but wasn't sure. So that is also the reason, why tmpMatrix changes in the for loops, right ? So this is not a new variable, but only a new varible name.

    So, behind the variable names a and b in your example is same reference (or address) ?

  • But how can I copy only the value ?

  • Answer ✓

    double nested loop... like line 19 but without swapping i and j

  • Answer ✓

    stealing this from stack overflow:

    http://www.journaldev.com/753/how-to-copy-arrays-in-java

    not sure how they work with 2d arrays

  • Thanks a lot. Have a nice weekend.

  • edited January 2018 Answer ✓

    I've created a utility function which copies the whole content of a 2D-Array (Matrix) to another 1 based on arrayCopy(): :\">
    https://Processing.org/reference/arrayCopy_.html

    You can see its console output online as well: :bz
    https://OpenProcessing.org/sketch/496288

    However you need to click at the right bottom icon in order to open the output console though: :-\"

    /**
     * MatrixCopy (v1.0)
     * GoToLoop (2018/Jan/12)
     *
     * Forum.Processing.org/two/discussion/25921/
     * problems-with-transpose-a-2d-array-or-matrix#Item_7
     *
     * OpenProcessing.org/sketch/496288
     */
    
    final int[][] matrix = {
      {0, 0, 0}, 
      {0, 1, 0}, 
      {1, 1, 1}
    };
    
    final int[][] cloned = new int[3][3];
    
    void setup() {
      matrixCopyInt(matrix, cloned);
    
      showMatrixInt(matrix);
      println();
    
      showMatrixInt(cloned);
      exit();
    }
    
    static final void showMatrixInt(final int[][] arr2d) {
      for (final int[] arr1d : arr2d)  println(str(arr1d));
    }
    
    static final void matrixCopyInt(final int[][] src2d, final int[][] dst2d) {
      for (int len = min(src2d.length, dst2d.length), i = 0; i < len; ++i) {
        final int[] src1d = src2d[i], dst1d = dst2d[i];
        final int items = min(src1d.length, dst1d.length);
        arrayCopy(src1d, dst1d, items);
      }
    }
    
  • Wow ! Nice. Thank you. This helps me a lot.

Sign In or Register to comment.