Natalia Sacco :
I'm importing some values from matlab in a java code (Eclipse). I want to change the index of the matrix with the lists that i have in java. How can i do that?
'''
public class ConMat {
public static void main(String[] args) throws Exception {
int n = 2;// number of requests
int K=2;
int F=2*n;
int N=2*n;
LinkedList<Integer> Origin = new LinkedList<Integer>();
Origin.add(0,1);
Origin.add(1,2);
Origin.add(2,3);
LinkedList<Integer> Dest = new LinkedList<Integer>();
Dest.add(0,2);
Dest.add(1,3);
Dest.add(2,1);
LinkedList<Integer> U = new LinkedList<Integer>();
U.addAll(Origin);
U.addAll(Dest);
LinkedList<Integer> Z = new LinkedList<Integer>();
Z.addAll(Origin);
Z.addAll(Dest);
// Z.add(4,0);
// Z.add(5,5);
MatlabEngine eng1 = MatlabEngine.startMatlab();
eng1.eval("addpath('.\\MATLAB\\Dial')");
int D=3;
double[][] OD_incr=new double[D][D] ;
double[][]odmatrix=new double[D][D];
double [] origins= {1,2,3};
double [] destinations= {2,3,1};
double []scaling= {2};
double[][]roots = eng1.feval("Dial_java",OD_incr,origins,destinations,scaling);
System.out.println(Arrays.deepToString(roots));
I obtain this:
[[0.5, 0.4791428571428572, 0.0], [0.0, Infinity, Infinity], [0.4791428571428572, 0.0, Infinity]]
I want to create a matrix with these values and the the index equals to Origin and Dest.
For example:
double[][] c = new double[][];
for( int i:Origin){
for(int j:Dest){
c[i][j]=roots[i][j];
}
}
How can i do that?
WJS :
I'm not quite certain I understand. But matrices in Java are 0 based. So you would need to subtract one from the matlab matrix index.
Try this. I'm not certain how you want to apply Origin and Dest.
int D = 3;
double[][] c = new double[D][D];
for( int i= 0; i < D; i++){
for (int j = 0; j < D; j++) {
c[Dest.get(i)-1][Dest.get(j)-1]
=roots[Origin.get(i)-1][Origin.get(j)-1];
}
}
System.out.println(Arrays.deepToString(c));
Here is a sample of what I was doing so you can apply the technique yourself. See if this does what you want. It uses sample data from your post since I don't have MatLab.
int D = 3;
double roots[][] = { { 0.5, 0.4791428571428572, 0.0 },
{0.0,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY},
{ 0.4791428571428572, 0.0, Double.POSITIVE_INFINITY, } };
System.out.println("Before copying");
for (double[] a : roots) {
System.out.println(Arrays.toString(a));
}
double[][] c = new double[D][D];
for (int i = 0; i < D; i++) {
for (int j = 0; j < D; j++) {
c[Dest.get(i)-1][Dest.get(j)-1] = roots[Origin.get(i)-1]
[Origin.get(j)-1];
}
}
System.out.println("\nAfter copying");
for (double[] a : c) {
System.out.println(Arrays.toString(a));
}