通过获取平均值不调整工作在C#中int数组? [英] resize int array in C# by getting average doesn't work?

查看:129
本文介绍了通过获取平均值不调整工作在C#中int数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code调整大小的数组新的大小:

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

我想获得一个int数组,然后返回与通过Lastarray的项目平均取得了新的尺寸新的数组。
(例如调整图像大小,以新的大小)

    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:

最后一个数组:
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

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

新的数组:
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

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

另外最后一个数组:
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 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

另一个新的数组:
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

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

推荐答案

您不能只挑值这样的,总结起来,你需要做的每一个旧值的分数进行平均计算,你要投入新的价值观。看看重采样的算法,例如 http://drdobbs.com/184405045

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();
    }

这篇关于通过获取平均值不调整工作在C#中int数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆