有没有一种方法可以复制一个矩阵,但使用不同的索引?

huangapple 未分类评论47阅读模式
英文:

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&lt;Integer&gt; Origin = new LinkedList&lt;Integer&gt;();
	        Origin.add(0,1);
	        Origin.add(1,2);
	        Origin.add(2,3);
LinkedList&lt;Integer&gt; Dest = new LinkedList&lt;Integer&gt;();
	        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 &lt;N; i++){
           e[Origin.get(i)-1]=new double[N];
           for (int j = 0; j &lt;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?

huangapple
  • 本文由 发表于 2020年4月7日 07:08:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/61070336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定