Reputation: 143
I have an n by 2 array 'ppi' having integer values(with duplicate), a second array n by 1 'gid' containing unique integer values from 'ppi' and a third array 'X0' with 0 and 1 values. Below is a MATLAB code which I want to implement in Java-
MATLAB version-
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 code for the same implementation
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)
}
Can someone please tell me why the values in mrfpdf array is different in Java and Matlab code? Java code results in 0.99 and 0.5 values only whereas Matlab does not have any 0.5 value
Thanks
Upvotes: 0
Views: 377
Reputation: 36710
Your matlab-code uses doubles while java-code uses integers (e.g. u0 and u1)
Upvotes: 1