ruby 1.9 中如何反转矩阵对象?
我是 Ruby 的初学者。所以我搜索了如何反转我尝试过的矩阵
1, 2, 3, 4 8, 9, 10, 11
4, 5, 6, 7 => 4, 5, 6, 7
8, 9, 10, 11 1, 2, 3, 4
:
require 'matrix'
m = Matrix[ [1,2,3,4],[4,5,6,7], [8,9,10,11] ]
m_rev = Matrix.build(m.row_size, m.column_size){|row|
m.row_size.downto(0){|i|
row = m.row(i)
}
}
I am a beginner with Ruby. So I searched how reverse a matrix
1, 2, 3, 4 8, 9, 10, 11
4, 5, 6, 7 => 4, 5, 6, 7
8, 9, 10, 11 1, 2, 3, 4
I have tried:
require 'matrix'
m = Matrix[ [1,2,3,4],[4,5,6,7], [8,9,10,11] ]
m_rev = Matrix.build(m.row_size, m.column_size){|row|
m.row_size.downto(0){|i|
row = m.row(i)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
Matrix[*m.to_a.reverse]
:不要错过
*
。Try:
Matrix[*m.to_a.reverse]
:Do not miss the
*
.你可以这样做:
You can do something like:
您走在正确的轨道上,除了
Matrix.build
迭代行和列:更简单、更快:
You were on the right track, except that
Matrix.build
iterates over both rows and columns:Simpler and faster: