• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1569
  • Last Modified:

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

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:
Array.Copy(MyArr1,1,MyArr2,0,3)
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?
0
Cluskitt
Asked:
Cluskitt
  • 2
  • 2
1 Solution
 
TommySzalapskiCommented:
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.
Next
0
 
CluskittAuthor Commented:
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.
Next

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.
0
 
TommySzalapskiCommented:
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.
0
 
CluskittAuthor Commented:
Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now