Convert each row of the matrix (float) to a vector

I have an array of float [n, 128]. Now I want to convert each line into a separate vector as follows:

// The code here is pseudo code

int n=48; float[,] arrFloat=new float[n,128]; VectorOfFloat v1 = new VectorOfFloat(128); // Vn equals to number of n v1= arrFloat[0]; v2=arrFloat[1] . . . . Vn 

What is the best way?


I could write the code as follows, but I think there should be a better way:

  List<VectorOfFloat> descriptorVec = new List<VectorOfFloat>(); VectorOfFloat v1 = new VectorOfFloat(); var temp = new float[128]; for (int i = 0; i < descriptors1.GetLength(0); i++) { for (int j = 0; j < descriptors1.GetLength(1); j++) { temp[j] = descriptors1[0, j]; } v1.Push(temp); descriptorVec.Add(v1); } 
+4
source share
1 answer

You can use Buffer.BlockCopy or ArrayCopy instead of manually assigning each value to

  static float[][] ToVectors(float[,] matrix) { var array = new float[matrix.GetLength(0)][]; for (int i = 0; i < array.Length; i++) { array[i]=new float[matrix.GetLength(1)]; Buffer.BlockCopy(matrix, i * matrix.GetLength(1), array[i], 0, matrix.GetLength(1) * sizeof(float)); } return array; } 
+3
source

Source: https://habr.com/ru/post/1445687/


All Articles