java - MATLAB的interp1函数在J2ME中的实现

标签 java matlab java-me interpolation linear-interpolation

我希望在 J2ME 或 JAVA 中实现 interp1、一维数据插值(表查找)、MATLAB 中可用的函数。这是链接

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/interp1.html

J2ME或JAVA中是否有库可以实现相同的功能?如果没有,有人可以帮助我在 J2ME 或 JAVA 中实现 interp1 功能吗?

最佳答案

示例 code taken from here (仅限线性):

public static final double[] interpLinear(double[] x, double[] y, double[] xi) throws IllegalArgumentException {

        if (x.length != y.length) {
            throw new IllegalArgumentException("X and Y must be the same length");
        }
        if (x.length == 1) {
            throw new IllegalArgumentException("X must contain more than one value");
        }
        double[] dx = new double[x.length - 1];
        double[] dy = new double[x.length - 1];
        double[] slope = new double[x.length - 1];
        double[] intercept = new double[x.length - 1];

        // Calculate the line equation (i.e. slope and intercept) between each point
        for (int i = 0; i < x.length - 1; i++) {
            dx[i] = x[i + 1] - x[i];
            if (dx[i] == 0) {
                throw new IllegalArgumentException("X must be montotonic. A duplicate " + "x-value was found");
            }
            if (dx[i] < 0) {
                throw new IllegalArgumentException("X must be sorted");
            }
            dy[i] = y[i + 1] - y[i];
            slope[i] = dy[i] / dx[i];
            intercept[i] = y[i] - x[i] * slope[i];
        }

        // Perform the interpolation here
        double[] yi = new double[xi.length];
        for (int i = 0; i < xi.length; i++) {
            if ((xi[i] > x[x.length - 1]) || (xi[i] < x[0])) {
                yi[i] = Double.NaN;
            }
            else {
                int loc = Arrays.binarySearch(x, xi[i]);
                if (loc < -1) {
                    loc = -loc - 2;
                    yi[i] = slope[loc] * xi[i] + intercept[loc];
                }
                else {
                    yi[i] = y[loc];
                }
            }
        }

        return yi;
    }

关于java - MATLAB的interp1函数在J2ME中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2986650/

相关文章:

Java,获取实现特定接口(interface)的 URLClassLoader 可用的所有类

角度的matlab方差

matlab - 如何在 Matlab 中使用多重线性函数?

graphics - 透明位图

java - J2ME发送短信时出现NullPointer异常

java - 如何通过按钮更改操作栏的颜色?

Java 桌面应用程序 - 哪种技术?

java - 处理扭矩时如何正确定义项目结构

c# - 如何在 C# 中创建可供 MATLAB 使用的 .net 程序集?

java - 在带有 Netbeans 7.3 的诺基亚 SDK 2.0 中找不到 JRE