I want to swap either rows or columns in an array of arrays (i.e. a matrix). I have found this swap method online, and I extended it with my mutate
function, where the board.matrix
is the array of arrays:
我想在数组数组(即矩阵)中交换行或列。我在网上发现了这个交换方法,我用我的mutate函数扩展了它,其中board.matrix是数组的数组:
# Swap to elements of an Array
def swap!(a,b)
self[a], self[b] = self[b], self[a]
self
end
def mutate(board)
matrix = board.matrix
random = rand
rand_a = rand(matrix.length-1)
rand_b = rand(matrix.length-1)
puts "Old one:"
board.print_matrix
# We have a 50:50 chance of swapping either columns or rows
if random <= 0.5
# Swap columns: Transpose first
puts "Swapping columns #{rand_a} and #{rand_b}..."
temp = matrix.transpose
temp.swap!(rand_a, rand_b)
matrix = temp.transpose
else
# Just swap rows
puts "Swapping rows #{rand_a} and #{rand_b}..."
matrix.swap!(rand_a, rand_b)
end
puts "New one:"
board.print_matrix
end
Now it does what it should for rows:
现在它为行做了它应该做的事情:
Old one:
X X 0 0
0 0 0 0
X X 0 0
0 0 0 0
Swapping rows 1 and 0...
New one:
0 0 0 0
X X 0 0
X X 0 0
0 0 0 0
But it doesn't for columns. Why is that?
但它不适用于列。这是为什么?
Old one:
0 X X 0
0 0 X 0
X 0 0 0
0 0 0 0
Swapping columns 1 and 0...
New one:
0 X X 0
0 0 X 0
X 0 0 0
0 0 0 0
2 个解决方案
#1
2
Because in the following code you did not transformed the Array matrix
is pointing to, you just have assigned a new Array to matrix
, which is a local variable:
因为在下面的代码中你没有转换Array矩阵所指向的,你只需要为矩阵分配一个新的Array,这是一个局部变量:
temp = matrix.transpose
temp.swap!(rand_a, rand_b)
matrix = temp.transpose
You can actually replace this code with this:
您实际上可以用以下代码替换此代码:
matrix.each do |row|
row[rand_a], row[rand_b] = row[rand_b], row[rand_a]
end
#2
2
The problem is that you only set the local variable matrix
to the new object created by #transpose
.
问题是您只将局部变量矩阵设置为#transpose创建的新对象。
Then you printed the original matrix object which is apparently board.matrix
.
然后你打印出原来的矩阵对象,显然是board.matrix。
Although you also used the local variable to swap the rows, in that case the local was a reference to the original object, so they were the same, and it "worked" from the point of view of your output routine. But with the columns a new object was returned by transpose
and board
was never updated.
虽然您还使用局部变量来交换行,但在这种情况下,本地是对原始对象的引用,因此它们是相同的,并且从输出例程的角度来看它“起作用”。但是对于列,transpose返回了一个新对象,并且从未更新过board。
#1
2
Because in the following code you did not transformed the Array matrix
is pointing to, you just have assigned a new Array to matrix
, which is a local variable:
因为在下面的代码中你没有转换Array矩阵所指向的,你只需要为矩阵分配一个新的Array,这是一个局部变量:
temp = matrix.transpose
temp.swap!(rand_a, rand_b)
matrix = temp.transpose
You can actually replace this code with this:
您实际上可以用以下代码替换此代码:
matrix.each do |row|
row[rand_a], row[rand_b] = row[rand_b], row[rand_a]
end
#2
2
The problem is that you only set the local variable matrix
to the new object created by #transpose
.
问题是您只将局部变量矩阵设置为#transpose创建的新对象。
Then you printed the original matrix object which is apparently board.matrix
.
然后你打印出原来的矩阵对象,显然是board.matrix。
Although you also used the local variable to swap the rows, in that case the local was a reference to the original object, so they were the same, and it "worked" from the point of view of your output routine. But with the columns a new object was returned by transpose
and board
was never updated.
虽然您还使用局部变量来交换行,但在这种情况下,本地是对原始对象的引用,因此它们是相同的,并且从输出例程的角度来看它“起作用”。但是对于列,transpose返回了一个新对象,并且从未更新过board。