gpt4 book ai didi

java - Java 和 Matlab 中给出不同结果的指数函数

转载 作者:行者123 更新时间:2023-12-01 14:00:37 25 4
gpt4 key购买 nike

我有一个 n x 2 数组“ppi”,具有整数值(有重复),第二个数组 n x 1“gid”,包含“ppi”中的唯一整数值​​,第三个数组“X0”,包含 0 和 1 值。下面是我想用 Java 实现的 MATLAB 代码-

MATLAB 版本-

    gamma0 = 10; gamma1 = 10; beta = 20;
delta = 0;weight=10;
for i=1:length(gid)
gconn = union(ppi(find(ppi(:,1)==gid(i)),2), ppi(find(ppi(:,2)==gid(i)),1));
[a,b] = intersect(gid, gconn);
u1 = (X0(i)*weight + sum(X0(b)==1))/(weight+length(gconn));
u0 = ((1-X0(i))*weight + sum(X0(b)==0))/(weight+length(gconn));
mrfpdf(i) = exp((1-X0(i))*(gamma0-beta*u1)+X0(i)*(gamma1-beta*u0))/(exp(gamma0- beta*u1)+exp(gamma1-beta*u0));
end

相同实现的Java代码

    double[] mrfpdf = new double[X0.length];
Integer[] X = new Integer[X0.length];

// find out unique class labels in ppi
Set<Double> gid_set = new LinkedHashSet<Double>();
for (int i = 0; i < ppi.length; i++) {
for (int j = 0; j < ppi[0].length; j++) {
gid_set.add(ppi[i][j]);
}
}
System.out.println("unique gid_set: " + gid_set);
Double[] gid = gid_set.toArray(new Double[gid_set.size()]);
Arrays.sort(gid);

int gamma0 = 10, gamma1 = 10, beta = 20, delta = 0;weight=10;
for (int i = 0; i < gid.length; i++) {

Set<Double> ppi1 = new HashSet<Double>();
Set<Double> ppi2 = new HashSet<Double>();
for (int k = 0; k < ppi.length; k++) {
if (ppi[k][0] == gid[i])
ppi1.add(ppi[k][1]);

if (ppi[k][1] == gid[i])
ppi2.add(ppi[k][0]);

}

Set<Double> gconn = new HashSet<Double>(ppi1);
gconn.addAll(ppi2);
Set<Double> a = new HashSet<Double>(gid_set);
a.retainAll(gconn);
java.util.List<Double> buff = new LinkedList<Double>(gid_set);
int[] b = new int[a.size()];// get index of intersection elements in this array
int x = 0;
for (double j : a) {
for (int c = 0; c < gid.length; c++) {
if (j == gid[c]) {
b[x] = c;
x++;
}
}
}
java.util.List<Double> X0_list = new ArrayList<Double>();
for (double z : X0) {
X0_list.add(z);
}
int sum1 = 0;
for (int y : b) {
if (X0_list.get(y) == 1)
sum1++;
}
int sum2 = 0;
for (int y : b) {
if (X0_list.get(y) == 0)
sum2++;
}
u1 = (int) ((X0[i] * weight + sum1) / (weight + gconn.size()));
u0 = (int) (((1 - X0[i]) * weight + sum2) / (weight + gconn.size()));
mrfpdf[i] = (Math.exp((1 - X0[i]) * (gamma0 - beta * u1) + X0[i]* (gamma1 - beta * u0)) / (Math.exp(gamma0 - beta * u1) + Math.exp(gamma1 - beta * u0)));

}// end for(int i : gid)
}

有人可以告诉我为什么 mrfpdf 数组中的值在 Java 和 Matlab 代码中不同吗? Java 代码仅产生 0.99 和 0.5 值,而 Matlab 没有任何 0.5 值

谢谢

最佳答案

您的 matlab 代码使用 double ,而 java 代码使用整数(例如 u0 和 u1)

关于java - Java 和 Matlab 中给出不同结果的指数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com