• Advertisement
Sign in to follow this  

[.net] Reshape Array in C#

This topic is 3312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement