这个问题可能很简单,我可能确实缺少一些基本知识,但是如何在C#中内插一维数组?
让我们说我有n个元素组成的数组
package classwork7_2;
import java.util.*;
import java.io.*;
public class ClassWork7_2 {
public static void main(String[] args)throws IOException {
Scanner s = new Scanner(System.in);
int[] numbers = fileToArray();
Arrays.sort(numbers);
char quit = 'q';
while (true) {
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in file\n");
} else {
System.out.print("Number is in file\n");
}
}
}
public static int[] fileToArray() throws IOException{
Scanner s = new Scanner(System.in);
int[] array = new int[7];
System.out.print("Enter name of file: ");
String filename = s.nextLine();
File f = new File(filename);
Scanner inputFile = new Scanner(f);
int i = 0;
while(inputFile.hasNext()){
array[i] = inputFile.nextInt();
i++;
}
inputFile.close();
return array;
}
}
如何拉伸或压缩数组以使其具有n个值并对其进行插值,就像调整图像大小时那样,就是这样,而不是对数组进行斩波或添加零或空值。
例如,如果我要转换数组,使其具有n = 4个元素,请获取此
int[] array1 = new int[] { 1, 3, 5, 7, 1 };
我要尝试的操作与matlab的重采样功能相同 https://mathworks.com/help/signal/ref/resample.html
答案 0 :(得分:2)
对于新数组比旧数组短的情况,我建议采用这种解决方案:
int[] array1 = new int[] { 1, 3, 5, 7, 9 };
int[] array2 = new int[4];
for (var i = 0; i < array2.Length; i++)
{
var doubleIndex1 = (double)i * array1.Length / array2.Length;
var index1 = (int)Math.Floor(doubleIndex1);
var rel = doubleIndex1 - index1;
array2[i] = (int)Math.Round((1.0 - rel) * array1[index1] + rel * array1[index1 + 1]);
}