Reputation: 145
I have a 2D array which looks like this:
0 1 2 3 4
0 [4, 5, 6, 7, 9]
1 [9, 2, 1, 6, 4]
2 [0, 0, 0, 0, 0]
3 [8, 7, 6, 4, 1]
4 [0, 0, 0, 0, 0]
I would like to change the order of the rows and then change the columns so they match up to the original order i.e. (1,1) would stay as 2. So when I swap the rows I get:
0 1 2 3 4
2 [0, 0, 0, 0, 0]
4 [0, 0, 0, 0, 0]
0 [4, 5, 6, 7, 9]
1 [9, 2, 1, 6, 4]
3 [8, 7, 6, 4, 1]
I am able to do this in code. But what I can't wrap my head around is how to change the columns according to the switch. So in the end I would like:
2 4 0 1 3
2 [0, 0, 0, 0, 0]
4 [0, 0, 0, 0, 0]
0 [6, 9, 4, 5, 7]
1 [1, 4, 9, 2, 6]
3 [6, 1, 8, 7, 4]
Where the original row -> column indices mapping remains. Could anyone give me some tips on how to approach this problem?
Upvotes: 1
Views: 135
Reputation: 78
Remember that a 2D array is just an array of arrays. What you are calling columns are not actually a part of the structure, rather just the same spot in each of the arrays. To switch a column, you need to switch the same spot in each of the arrays.
Upvotes: 0
Reputation: 960
Here is my suggestion for some pseudocode which should solve your problem:
By never deleting the old table, you can very easily reference back to it to repopulate your new table.
Upvotes: 1