Extract vector from 2d array efficiently in C #

I have a very large two-dimensional array and I need to compute vector operations on this array. NTerms and NDocs are very large integers.

var myMat = new double[NTerms, NDocs];

I need to extract vector columns from this matrix. I am currently using for loops.

            col = 100;
            for (int i = 0; i < NTerms; i++)
            {
                myVec[i] = myMat[i, col];
            }

      

This operation is very slow. In Matlab, I can extract a vector without the need for iteration:

myVec = myMat[:,col];

      

Is there a way to do this in C #?

+3


source to share


2 answers


There are no constructs in C # that will allow you to work with arrays like in Matlab. With the code you already have, you can speed up the vector creation process by using the Task Parallel Library , which was introduced in the .NET Framework 4.0.

Parallel.For(0, NTerms, i => myVec[i] = myMat[i, col]);

      

If your processor has more than one core, you will get some performance improvement, otherwise there will be no effect.



For more examples of how you can use the parallel library of matrix and array tasks, see the MSDN article Matrix Decomposition .

But I doubt C # is a good choice when it comes to some serious math.

+3


source


Some possible problems:

Could this be element access for multidimensional arrays in C #. See this earlier article .



Another problem might be that you are accessing non-contiguous memory - so there is not much help from the cache, and you might even have to fetch from virtual memory (disk) if the array is very large.

What happens to your speed when you access an entire row at a time, rather than a column? If it is significantly faster, you can be 90% sure that there is an offset memory problem ...

0


source







All Articles