Best way to split two dimensional arrays into smaller ones (VB.Net)

Posted on 2012-08-24
Last Modified: 2012-08-27
Let's imagine I have an array MyArr1(5, 5). Type isn't important. What would be the best way to split this array into a smaller one? For example, I want MyArr2(3, 3) to be the same as MyArr1(1 to 3, 2 to 4).

If it were a one dimensional array, I could just do:
But I don't see how that could work for multi-dimension arrays.

The only way I can think of it to create a loop and populate on a one by one basis. Don't know if there's a better way to do this, but for larger dimension arrays it seems like a very inefficient method.

Any ideas on the best way to tackle this?
Question by:Cluskitt
    LVL 37

    Expert Comment

    The way arrays are stored in memory make it so Array.Copy is quite efficient for 1D arrays. It also makes it so there is no super-efficient way to copy pieces of 2D arrays.
    If you are copying the entire array, then it can be efficient, but with chunks like that, you're not hurting anything using loops.

    Now, don't do them one-by-one with a nested loop though. Make one loop and use Array.Copy to copy the 1D arrays. (Think of a 2D array as a 1D array of 1D arrays and it should make sense).

    Forgive the syntax (I'm not a VB guy) but something like this
    For i = 1 to ArrayHeight
      Array.Copy(MyArr1(i),1,MyArr2(2+i),0,3) 'Trying to offset so 1,1 starts at 3,0.
    LVL 18

    Author Comment

    Actually, MyArr2 should start at 0. I think what you're suggesting would be more like:
    For i = 1 to 3
      Array.Copy(MyArr1(i),2,MyArr2(i-1),0,3) 'Trying to offset so 1,1 starts at 3,0.

    This would loop "columns" 2 to 4 and "rows" 1 to 3 of MyArr1 and place them on MyArr2(0 to 2, 0 to 2)

    I suppose, if I had a 3 dimensional array, I would still have to do a nested loop. This would just save one loop.

    I was thinking more on the terms of some sort of Redim, actually. If I wanted to simply cut the last rows/cols, I would redim to a smaller number. I wonder if there's a way to do that for the first rows/cols... maybe with reverse? I'm really not sure how reverse works on multi-dimensional arrays, if at all.
    LVL 37

    Accepted Solution

    The reason Array.Copy works so well is because arrays are stored in contiguous memory blocks. Multidimensional arrays are still stored like that so row 2 starts (in memory) right after row 1 ends.

    So you can see that you can't really ReDim without moving the entire array anyway. ReDim isn't going to be any more efficient (especially in terms of run-time). So just go with the nested loops. You can probably save one loop using Array.Copy and if those parts are large, you would see a performance increase (unless the optimizer does that for you anyway).

    In short: You're not going to do any better than the loops for anything but the last dimension.
    LVL 18

    Author Closing Comment


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now