matrix - Replace value of a Vector in Ruby -
i have matrix , want replace values of column (or row) single value.
i tried this:
m = matrix.empty(5, 0) n = matrix.empty(0, 5) g = m *n puts g.column(3).map! { 3 }
but map!
isn't working vector, , map
doesn't change values of column in matrix. how can this?
g.column(3).class => vector vector.instance_methods.grep(/map/) => [:map, :map2, :flat_map]
vector haven't map!
method.
g[0, 0] =3 nomethoderror: private method `[]=' called #<matrix:0x007f8cd8951d18>
[]=
private method, can use send
method bypass private:
g.send(:[]=, 0, 0, 3) => 3 g = matrix[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] 5.times |i| g.send(:[]=,i, 3, 3) end g => matrix[[0, 0, 0, 3, 0], [0, 0, 0, 3, 0], [0, 0, 0, 3, 0], [0, 0, 0, 3, 0], [0, 0, 0, 3, 0]] irb(main):580:0> 5.times |i| irb(main):581:1* g.send(:[]=, 0, i, 3) irb(main):582:1> end => 5 irb(main):583:0> g => matrix[[3, 3, 3, 3, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Comments
Post a Comment