4

我想要做的就是使用matrix[i][j]循环交换。为什么这不起作用?matrix[j][i]while

def my_transpose(matrix)

  new_matrix = []

  i = 0
  j = 0

  while i < matrix.size
    new_matrix[i] = []
    while j < matrix.size
      new_matrix[i] << matrix[j][i]
      j += 1
    end
    i += 1
  end

  return new_matrix
end

如果我用类似的东西运行它

[
[1,2,3],
[1,2,3],
[1,2,3]
]

它只是返回1,1,1。我如何让它返回1,1,1; 2,2,2; 3,3,3

4

4 回答 4

16

如果您的问题是如何使用 Ruby 交换矩阵中的列和行,答案是使用内置的Array#transpose

a = [
[1,2,3],
[1,2,3],
[1,2,3]
]
#=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
a.transpose
#=> [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
于 2013-10-08T02:38:23.770 回答
3

j = 0i循环内移动

def my_transpose(matrix)

  new_matrix = []

  i = 0


  while i < matrix.size
    new_matrix[i] = []
    j = 0  # move this here
    while j < matrix.size
      new_matrix[i] << matrix[j][i]
      j += 1
    end
    i += 1
  end

  return new_matrix
end

如果j没有为每个循环重置为 0,i则它永远不会进入j循环,除非是第一次:

i = 0
j = 0
# Enter i loop
 new_matrix[0] = []
 # Enter j loop
  new_matrix[0] << matrix[0][0] 
  j += 1 #=> 1

  new_matrix[0] << matrix[1][0] 
  j += 1 #=> 2

  new_matrix[0] << matrix[2][0] 
  j += 1 #=> 3
 # Exit j loop
 i += 1 #=> 1

 new_matrix[1] = []
 # Does not enter j loop as j = 3 > matrix.size
 i += 1 #=> 2

 new_matrix[2] = []
 # Does not enter j loop as j = 3 > matrix.size
 i += 1 #=> 3
# Exit i loop
于 2013-10-08T02:31:54.577 回答
1

您可以为此使用并行分配,例如a,b = b,a。附带说明一下,while循环在 Ruby 中很少使用,将您视为菜鸟。一种更类似于 Ruby 的方式是使用枚举器,如timesuptodownto等,它们可以方便地提供索引ij作为循环变量。当您将方法存储在 Array 类中时,您还可以保存自己以matrix一遍又一遍地键入,因为您已经在该类中。此外,通常先编写一个 mutator 方法(更改原始对象,通常!在方法名称的末尾用一个 bang 表示),然后编写一个附加的 non-mutator 方法,该方法只是在副本上调用 mutator。这是我编写代码的方式:

class Array
  def my_transpose!
    size.times do |i|
      0.upto(i) do |j|                                   # iterate only through lower half
        self[i][j], self[j][i] = self[j][i], self[i][j]  # swap rows and cols
      end
    end
    self                                                 # return the array itself
  end

  def my_transpose
    dup.map(&:dup).my_transpose!                         # inner arrays are dup'ed, too
  end
end

有趣的是,Ruby 的内置transpose函数不能用作 mutator。以下是如何使用上面的代码:

a = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

a.my_transpose!                        # mutator
#=> [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

a
#=> [[1, 1, 1], [2, 2, 2], [3, 3, 3]]  # note that a changed

a.my_transpose!
#=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

a
#=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]  # a is back to its original state

a.my_transpose                         # non-mutator
#=> [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

a
#=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]  # note that a did not change
于 2013-10-08T07:55:28.247 回答
0

当您转置 amxn 矩阵时,列变为行,反之亦然。如果数组的行数和列数不同,则需要从 i 迭代到 matrix[0].size,因为每个元素中的列数需要是转置矩阵中的行数。

def my_transpose(matrix)

  new_matrix = []

  i = 0


  while i < matrix[0].size # this will create correct number of rows
    new_matrix[i] = []
    j = 0
    while j < matrix.size
      new_matrix[i] << matrix[j][i]
      j += 1
    end
    i += 1
  end

  return new_matrix
end
于 2018-11-08T03:57:32.927 回答