I want to add a row to a two-dimensional NArray. The way described in NArray 0-7 Tutorial is pretty complex - and I wonder if there is a more simple way.
So if I have two NArrays:
n1 = [[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]
n2 = [16, 17, 18, 19]
I would like to add n1 and n2 to get n3:
n3 = [[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]]
How may this be done?
require "narray"
class NArray
def concat(other)
shp = self.shape
shp[1] += 1
a = NArray.new(self.typecode,*shp)
a[true,0...-1] = self
a[true,-1] = other
return a
end
end
n1 = NArray[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]
n2 = NArray[16, 17, 18, 19]
p n1.concat(n2)
# => NArray.int(4,5):
# [ [ 0, 1, 2, 3 ],
# [ 4, 5, 6, 7 ],
# [ 8, 9, 10, 11 ],
# [ 12, 13, 14, 15 ],
# [ 16, 17, 18, 19 ] ]