1
votes

I using this code to resize an array to new size:

I want to get an int array then return new array with new size that made by Lastarray's Items's average. (for example resize an Image to new size)

    private int[] ResizeArray(int[] LastArray, int NewSize)
    {
        if (LastArray.GetUpperBound(0) == (NewSize-1))
        {
            return LastArray;
        }
        int i = 0;
        int j = 0;
        int c = 0;
        int[] NewArray = new int[NewSize];
        for (i = 1; i < NewSize; i++)
        {
            c = 0;
            for (j = (int)(i * ((double)LastArray.GetUpperBound(0) / (NewSize))); j <= ((i + 1) * ((double)LastArray.GetUpperBound(0) / (NewSize)) - 1); j++)
            {
                c += LastArray[j];
            }
            NewArray[i] = (int)(c / ((double)LastArray.Length / (NewSize)));
        }
        return NewArray;
    }

but Some times It returns unreasonable numbers! Like this:

Last Array: 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365 365

New array: 0 421 280 421 280 421 280 421 421 280 421 280 421 280 421 421 280 421 280 421 280 421 421 280 421 280 421 280 421 421

another Last array: 83 84 89 90 91 92 93 94 95 96 97 99 100 101 102 104 105 106 109 111 112 114 116 118 120 121 123 125 127 128 130 132 133 134 136 137 138 140 141 142 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

another New array: 0 103 71 109 75 116 80 125 131 91 141 98 151 103 159 164 111 170 115 176 119 182 185 125 191 129 197 133 203 206

3
Is it only for array resize? looks like you are also doing some average operation which i don't follow. can you elaborate more what you want to acheive? - NirMH
@ahmadali So you want to resize the array and fill the elements with average of items (which items count for average is newSize) ? - fardjad
@fardjad:Yes but this code doesn't return true array - ahmadali shafiee
@Scott:It just return new array includes Last array's Item from 0 to NewSize.Not getting average!!! - ahmadali shafiee

3 Answers

3
votes

You can't just pick values like that and sum them up, you'll need to do some averaging and calculation of the fraction of each old value that you want to put into the new values. Take a look at 'resampling' algorithms, e.g. http://drdobbs.com/184405045

Here's a simple resampler which upsamples the first array and then downsamples it. Not very efficient, but perhaps easier to understand:

    private static int[] ResizeArray(int[] oldArray, int newSize)
    {
        int oldSize = oldArray.Length;
        int rectangle = newSize * oldSize;

        var upsampled = Enumerable.Range(0, rectangle).Select(k => oldArray[k / newSize]).ToArray();
        var downSampled = Enumerable.Range(0, newSize).Select(j => Enumerable.Range(0, oldSize-1).Select(i => upsampled[j * oldSize + i])).Select(g => g.Average());
        return downSampled.Select(i => (int)i).ToArray();
    }
0
votes

You do not initialize NewArray[0]. Then the code you show is invalid because the j loop instruction is incomplete.

0
votes

NewArray[0]==0 because you are starting your outer for-loop at i=1 instead of i=0.

The two different values appearing in the rest of the array are because some of the blocks have more elements of LastArray than others, since they don't divide up evenly. However, you are always dividing by the same number ((double)LastArray.Length / (NewSize)) to try to compute the average, which will not work. You should instead divide by the actual number of values you added to c, which is

(int)((i + 1) * ((double)LastArray.GetUpperBound(0) / (NewSize))) - 
   (int)(i * ((double)LastArray.GetUpperBound(0) / (NewSize)))