timmy
timmy

Reputation: 1

flip a 2d array

I'm trying to get a 2D array to a form, so I can work with it like I need. I just cant modify it correctly..

Say I have a 2D array:

0 1 0 1 0 
0 0 1 1 1 
0 0 0 3 0 
0 0 0 0 1 
0 0 0 0 0 

How can I flip it to its symmetrical, so at the moment position 0,1 is 1 and therefore position 1,0 would be 1?

does this make seance?

I need to do this so I can work out Euler tour

Upvotes: 0

Views: 970

Answers (3)

bhups
bhups

Reputation: 14875

Isn't it same as transposing a square matrix?


(for i=0; i < order; i++) {
  for(j =i; j < order; j++) {
   temp = arr[i][j];
   arr[i][j] = arr[j][i];
   arr[j][i] = temp;
 }
}

Upvotes: 0

Steffen
Steffen

Reputation: 2948

Something like this (pseudo code)?

for x = 0 to width
  for y = 0 to x-1
     swap( array[x][y], array[y][x] )

Although depending on what your environment is, there might be special methods to do just that.

Upvotes: 0

Rik Heywood
Rik Heywood

Reputation: 13972

You could just access it in a flipped way.

x = 3;
y = 5;

// Ask for x,y element
$normal = $myarray[$x][$y];         

// Ask for x,y in the flipped array by asking for y,x
$flipped_access = $myarray[$y][$x]; 

Upvotes: 1

Related Questions