英文:
Is there a way to copy a matrix but with different indices?
问题
我是一名初学者开发者,我在复制矩阵时遇到了问题。
我有两个具有以下值的列表:
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);
我有一个双重数组 c
:
[[0.5, 0.4791428571428572, 0.0], [0.0, Infinity, Infinity], [0.4791428571428572, 0.0, Infinity]]
我是用从 0 到 3 的普通索引 i 和 j 创建的这个双重数组。
现在我想要创建一个矩阵,其值来自于数组 c,但是索引来自于列表(i 对应 Origin,j 对应 Dest)。
我尝试了这段代码:
int N = 3;
double[][] e = new double[N][N];
for (int i = 0; i < N; i++) {
e[Origin.get(i) - 1] = new double[N];
for (int j = 0; j < N; j++) {
e[Origin.get(i) - 1][Dest.get(j) - 1] = c[i][j];
System.out.println(e[i][j]);
}
}
但是我得到了这个输出:
0.0
0.5
0.4791428571428572
0.0
0.0
Infinity
0.0
0.4791428571428572
0.0
有人可以帮助我吗?
英文:
I'm a beginner developer and i have a problem with the copy of a matrix.
I have two lists with the following values:
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);
and i have this double c [][]
[[0.5, 0.4791428571428572, 0.0], [0.0, Infinity, Infinity], [0.4791428571428572, 0.0, Infinity]]
I created this double with normal indices i and j from 0 to 3.
Now i would create a matrix with the values of c, but with the index of the lists (i for origin and j for Dest).
I i write this
int N=3;
double[][]e=new double[N][N];
for (int i = 0; i <N; i++){
e[Origin.get(i)-1]=new double[N];
for (int j = 0; j <N; j++){
e[Origin.get(i)-1][Dest.get(j)-1]=c[i][j];
System.out.println(e[i][j]);
i obtain this:
0.0
0.5
0.4791428571428572
0.0
0.0
Infinity
0.0
0.4791428571428572
0.0
Someone can help me?
专注分享java语言的经验与见解,让所有开发者获益!
评论