[.net] Reshape Array in C#

Started by
2 comments, last by TheTroll 15 years, 4 months ago
Hi, I have a 2 dimensional array of size (10, 10) and I would like to refer it as an array of size (2,50) WITHOUT copying its data, but keep the old reference to it as an array of size (10,10). How can I do it in C#? Thanks in advance, Yoav
Advertisement
Could always do it the old-fashioned way that n-dimensional arrays were done in languages that really only featured 1-dimensional arrays did it, which is to store it as a 1D array, and multiply to reach the correct entry. [just multiply by the pitch of one dimension, and add the offset of the other dimension].

You could make a class to handle the multiplication in the background, and just make a new access method that required supply of the dimensions of the array before indexing. Personally I'd do it with an extension method of the 1D array first, so you could keep all the other cool stuff that arrays could do, and just make it a 1D array under the covers.

This functionality doesn't already exist though, as far as I know anyway. Actually, I'd be very surprised if this is anywhere in any of the native .NET libraries, as this is a rather funky thing to require.
Thanks this is a possible solution and I will implement it I will not find a standard way to do it.

This is a very useful operation to do on matrices.
Such functions can be found in libraries which handle matrices and images (e.g. Matlab, OpenCV, etc.).

Just create your own class to hold the array. Overload the index operator [], and then you are done. Store the array as a single dimension array and then let the index operator take care of the fancy stuff under the hood. Now you will still have to have the overloaded index operator do the index math for you to make sure you get to the right one but from an outside point of view it would do exactly what you want it to do.

theTroll

This topic is closed to new replies.

Advertisement