[英]How can I convert an Integer array with length m to an array with length n in Java [SOLVED]
Hey want to convert an Integer array from a given length (bigger than 852) to an other Integer array of a lengh of 852.嘿想将 Integer 数组从给定长度(大于 852)转换为另一个长度为 852 的 Integer 数组。
That's because I want to display m values to 852 pixels.那是因为我想将 m 值显示为 852 像素。
The logic way is just to devide the ratio betwwen those two:逻辑方法只是将这两者之间的比率分开:
Let's say the array I want to compress has 2556 values.假设我要压缩的数组有 2556 个值。
The ratio between that is 2556/852 = 3, so I could jsut grab the 3 values and make the average.这之间的比率是 2556/852 = 3,所以我可以抓住 3 个值并取平均值。
The problem is that not allways the ratio is an integer, for example 2756/852 is 3.2347...问题是比率并非总是 integer,例如 2756/852 是 3.2347...
Can anyone help me?谁能帮我?
Thanks!谢谢!
I actually solved, I manage to create a function int Java that does exacly what I want.我实际上解决了,我设法创建了一个 function int Java,它完全符合我的要求。 You just need to import
import java.util.Arrays;
您只需要导入
import java.util.Arrays;
The function is compressArr(arrayToCompress, newArrayLength)
function 是
compressArr(arrayToCompress, newArrayLength)
Returns a float array.返回一个浮点数组。
public static float[] compressArr(float[] arr1, int l){
float[] arr2 = new float[l];
for(int i = 0; i<l;i++){
arr2[i] = average(Arrays.copyOfRange(arr1,P1(i,arr1.length,l),P2(i,arr1.length,l)+1));
}
return arr2;
}
public static int P1(int x,int n, int d){
float r = ((float)n)/d;
int b = 0;
int p = 0;
for(int i = 0; i<x ; i++){
p = (int)(x*r-b);
b*=p;
}
return(p);
}
public static int P2(int x,int n, int d){
x+=1;
float r = ((float)n)/d;
int b = 0;
int p = 0;
for(int i = 0; i<x ; i++){
p = (int)(x*r-b);
b*=p;
}
return(p-1);
}
static float average(float a[]) {
int n = a.length;
float sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
return sum / n;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.