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
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: