1

If I have a Narray with the shape 100, 10000 and want to expand it to say 100, 20000 (basically add rows) what is the proper way to achieve this? To expand massive Narrays I would like to avoid using a temporary Narray for memory reasons.

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 回答