Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing a 3D array

I am attempting to resize a 3D array in C#.

The VB6 code reads:

ReDim Combos(ProductNum, 4, 3)

Since I cannot use Array.Resize for a 3D array, is there another way for me to do this function in C#?

I have already declared the array as a 3D array:

int[ , , ] Combos;

But I am having trouble resizing it. Any ideas?

like image 787
eric_13 Avatar asked Jul 05 '12 16:07

eric_13


People also ask

How do you change the size of an array?

The shape of the array can also be changed using the resize() method. If the specified dimension is larger than the actual array, The extra spaces in the new array will be filled with repeated copies of the original array.

How do you resize a 3d image in Python?

To resize images in Python using OpenCV, use cv2. resize() method. OpenCV provides us number of interpolation methods to resize the image. Resizing the image means changing the dimensions of it.

How do you resize an array in Python?

With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.

What is the difference between reshape () and resize ()?

reshape() and numpy. resize() methods are used to change the size of a NumPy array. The difference between them is that the reshape() does not changes the original array but only returns the changed array, whereas the resize() method returns nothing and directly changes the original array.


2 Answers

There is no way to directly redimension a multidimensional array in .NET. I would recommend allocating a new array, and then copying the values into it using Array.Copy.

That being said, depending on what you're doing, you may want to consider using a different collection type, as well. There are more options in .NET than VB6 in terms of collections, so many cases which used multidimensional arrays are better suited towards other collection types, especially if they are going to be resized.

like image 70
Reed Copsey Avatar answered Oct 18 '22 02:10

Reed Copsey


Even for the one-dimensional array, in C#, the array resizing works by copying elements into new re-sized array.

If you need to re-size array often, collections would be much better solution.

If you still want to resize array, here is the code:

T[,,] ResizeArray<T>(T[,,] original, int xSize, int ySize, int zSize)
{
    var newArray = new T[xSize, ySize, zSize];
    var xMin = Math.Min(xSize, original.GetLength(0));
    var yMin = Math.Min(ySize, original.GetLength(1));
    var zMin = Math.Min(zSize, original.GetLength(2));
    for (var x = 0; x < xMin; x++)
        for (var y = 0; y < yMin; y++)
            for (var z = 0; z < zMin; z++)
                newArray[x, y, z] = original[x, y, z];
    return newArray;
}
like image 37
Dusan Avatar answered Oct 18 '22 02:10

Dusan