Making a matrix bigger

170 Views Asked by At

I'm using MatrixToolkitsJava for a Neural Network project for my thesis, and in it I need to make the weights matrices larger, and later potentially smaller. The way I'm currently doing this now, creating a new matrix and copying the existing values while instantiating the new values, is extremely inefficient, taking up the vast majority of the time. Is there an efficient way to add rows and columns to existing matrices?

1

There are 1 best solutions below

4
On

As stated you can use lists, but if you are dead set on using an array, you could create a method to create a new array, copy over the data, and then replace the reference that you are storing.

If you use System.arrayCopy(Object src, int srcPos, Object dest, int destPos, int length) it will fast enough if you aren't using a giant array.

EDIT: I can't believe I did not think of this earlier. Just use matrix multiplication to copy the data. For example, to make a 2 by 2 a 3 by 3, just do [2,2] x [(1,0), (0,1), (0,0)] = [2,3] => [(1,0,0), (0,1,0)] x [2,3] = [3,3]