Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 将行(数组)添加到空矩阵_Ruby_Arrays_Matrix_Row_Add - Fatal编程技术网

Ruby 将行(数组)添加到空矩阵

Ruby 将行(数组)添加到空矩阵,ruby,arrays,matrix,row,add,Ruby,Arrays,Matrix,Row,Add,我想在空矩阵中添加一行(数组) 与将数组添加到空数组类似: a = [] a << [1,2,3] => [[1,2,3]] 有什么想法吗?怎么样 m = [[1,2,3], [2,3,4]] matrix = Matrix.rows(m) m << [4,5,6] matrix = Matrix.rows(m) m=[[1,2,3],[2,3,4]] 矩阵=矩阵。行(m) m它在Ruby 1.9.3p392上运行良好 1.9.3p392 :001 >

我想在空矩阵中添加一行(数组)

与将数组添加到空数组类似:

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

=> [[1,2,3]]
有什么想法吗?

怎么样

m = [[1,2,3], [2,3,4]]
matrix = Matrix.rows(m)

m << [4,5,6]
matrix = Matrix.rows(m)
m=[[1,2,3],[2,3,4]]
矩阵=矩阵。行(m)

m它在Ruby 1.9.3p392上运行良好

1.9.3p392 :001 > require 'matrix'
 => true 
1.9.3p392 :002 > m = Matrix[]
 => Matrix.empty(0, 0) 
1.9.3p392 :003 > m = Matrix.rows(m.to_a << [1,2,3])
 => Matrix[[1, 2, 3]] 
1.9.3p392 :004 > m = Matrix.rows(m.to_a << [2,3,4])
 => Matrix[[1, 2, 3], [2, 3, 4]]
1.9.3p392:001>要求“矩阵”
=>正确
1.9.3p392:002>m=矩阵[]
=>矩阵。空(0,0)
1.9.3p392:003>m=矩阵行(m.to_a矩阵[[1,2,3]]
1.9.3p392:004>m=矩阵行(m.to_a矩阵[[1,2,3],[2,3,4]]
也可以使用2.0.0p0

2.0.0p0 :001 > require 'matrix'
 => true 
2.0.0p0 :002 > m = Matrix[]
 => Matrix.empty(0, 0) 
2.0.0p0 :003 > m = Matrix.rows(m.to_a << [1,2,3])
 => Matrix[[1, 2, 3]] 
2.0.0p0 :004 > m = Matrix.rows(m.to_a << [2,3,4])
 => Matrix[[1, 2, 3], [2, 3, 4]]
2.0.0p0:001>需要“矩阵”
=>正确
2.0.0p0:002>m=矩阵[]
=>矩阵。空(0,0)
2.0.0p0:003>m=矩阵行(m.to_a矩阵[[1,2,3]]
2.0.0p0:004>m=矩阵行(m.to_a矩阵[[1,2,3],[2,3,4]]
现在,要使其正常工作,请执行以下操作:

m = Matrix.rows(m.to_a << [1,2,3]) #=>Matrix[[1, 2, 3]]
p m #=>Matrix[[1, 2, 3]]
m=Matrix.rows(m.to_a Matrix[[1,2,3]]
p m#=>矩阵[[1,2,3]]

你的问题是什么?它对我有用。@JunZhou你使用的是哪个版本的ruby?在ruby 1.9.3中,结果与
OP
相同。虽然我知道为什么不能按预期工作?@RubyLovely我使用1.9.3p392。这些代码是作为答案发布的。请检查它。注意,从技术上讲,这是实例化一个新的矩阵,而不是“添加一行”到现有行。这可能会产生性能和微妙的语义含义,您应该记住。啊,我发现了问题。我没有将添加重新分配到矩阵(变量m)。现在它工作正常,谢谢。
1.9.3p392 :001 > require 'matrix'
 => true 
1.9.3p392 :002 > m = Matrix[]
 => Matrix.empty(0, 0) 
1.9.3p392 :003 > m = Matrix.rows(m.to_a << [1,2,3])
 => Matrix[[1, 2, 3]] 
1.9.3p392 :004 > m = Matrix.rows(m.to_a << [2,3,4])
 => Matrix[[1, 2, 3], [2, 3, 4]]
2.0.0p0 :001 > require 'matrix'
 => true 
2.0.0p0 :002 > m = Matrix[]
 => Matrix.empty(0, 0) 
2.0.0p0 :003 > m = Matrix.rows(m.to_a << [1,2,3])
 => Matrix[[1, 2, 3]] 
2.0.0p0 :004 > m = Matrix.rows(m.to_a << [2,3,4])
 => Matrix[[1, 2, 3], [2, 3, 4]]
require 'Matrix'
m = Matrix[]
p m.object_id #=> 6927492
p m.to_a.class #=> Array
p m.class #=> Matrix
p m.to_a.object_id #=> 6927384
p m.to_a << [1,2,3] #[[1, 2, 3]]
p m #=> Matrix.empty(0, 0)
p Matrix.rows(m.to_a << [1,2,3]).class #=> Matrix
p Matrix.rows(m.to_a << [1,2,3]).object_id #=> 6926640
p Matrix.rows(m.to_a << [1,2,3]) #=>Matrix[[1, 2, 3]]
p m #=> Matrix.empty(0, 0)
m = Matrix.rows(m.to_a << [1,2,3]) #=>Matrix[[1, 2, 3]]
p m #=>Matrix[[1, 2, 3]]