bluebox - 1 month ago 4x
Vb.net Question

# vb.net - Inserting 1-dimensional arrays into a 2-dimensional array

I'm working on an optimization algorithm and need to store some of the data (generated by the algorithm) in a 2-dimensional array called

`matrix`
, where row(i) contains the fitness score and parameter values of optimization vector(i).

``````Dim matrix(vectorCount() - 1, parameterCount()) As Double
Dim params(parameterCount() - 1) As Double

For i As Integer = 0 To vectorCount() - 1
matrix(i, 0) = vectorScore(i)
params = vectorValues(i)
For j As Integer = 0 To params.Length - 1
matrix(i, j+1) = params(j)
Next
Next
``````

int
`vectorCount()`
returns the number of vectors.

int
`parameterCount()`
returns the number of parameters in each vector.

double
`vectorScore(`
int
`vectorIndex)`
returns the fitness score of a specified vector.

double[]
`vectorValues(`
int
`vectorIndex)`
returns the parameter values of a specified vector.

My question:

Is there a faster (i.e. more efficient) way to insert
`params`
into
`matrix`
?

I always rely on a code like the one you wrote and haven't ever had a speed problem (this is really fast). I have done a quick research to make sure that there is no other option, but couldn't find any. The closest thing you have is `Array.Copy`, although it works just when the arrays have the same dimensions (I, personally, use it just with 1D arrays). In any case, I found an interesting link about Array.Copy() vs. loop performance for 2D arrays (it is in C# but everything is applicable to VB.NET).