1

形状が100、10000のNarrayがあり、それを100、20000(基本的に行を追加)に拡張したい場合、これを実現する適切な方法は何ですか?大規模なNarrayを拡張するために、メモリ上の理由から一時的なNarrayの使用は避けたいと思います。

4

1 に答える 1

1
require "narray"

class NArray
  def expand(*new_shape)
    na = NArray.new(self.typecode,*new_shape)
    range = self.shape.map{|n| 0...n}
    na[*range] = self
    return na
  end
end

p a = NArray.float(2,3).indgen!
# => NArray.float(2,3):
#    [ [ 0.0, 1.0 ],
#      [ 2.0, 3.0 ],
#      [ 4.0, 5.0 ] ]

p a.expand(3,4)
# => NArray.float(3,4):
#    [ [ 0.0, 1.0, 0.0 ],
#      [ 2.0, 3.0, 0.0 ],
#      [ 4.0, 5.0, 0.0 ],
#      [ 0.0, 0.0, 0.0 ] ]

移動せずにメモリブロックを拡張する一般的な方法はありません。メモリブロックは、十分な空き領域が続く場合にのみ拡張できますが、そのような場合は通常予期しないものです。

于 2012-03-14T12:19:16.527 に答える