This is inefficient. Create the NMatrix but without default values, since you know its final shape, and insert the values directly. You're doing array construction, array resizing, and then NMatrix initialization from that array, which is an order of magnitude more expensive than necessary.