Java矩阵中的值分配

时间:2018-07-25 09:10:13

标签: java arrays matrix while-loop jama

我想写一个基于Matrix的Java代码。我正在使用Jama矩阵。

代码如下:

import Jama.Matrix;
public class Landmarkassist {
public static void main(String args[]){
    double [] time1st
 {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};// Array of some 1st time slot.
    double[] time2nd={2.0,3.0,6.0,7.0,8.0};//Array of 2nd time slot
    double[] code={40.0,20.0,10.0,40.0,10.0};// Value of Marks
    Matrix Calculation=new Matrix(8,8);// A 8*8 Jama Matrix
    int k1=0,k2=0,l2=0,l1=0;
    while(k1<time1st.length && k2<time2nd.length){

        if(time1st[k1]<time2nd[k2]) {
       //Just to increment counter k1++
            k1++;
        }
   //If time slot 1 is equal to time slot 2 the we check for the codes if the codes matches with our criteria we update the cell of Jama matrix
        else if(time1st[k1]==time2nd[k2]){
            if(code[l1]==40.0){
                Calculation.set(l2,k1,40.0);//set the value to a specific cell of Jama Matrix l2 reperesent row number and k1 represent column no, and 40.0 is the value of that Cell
                l2++;
            }
            else if(code[l1]==20.0){
                Calculation.set(l2,k1,20.0);
                l2++;
            }
            else if(code[l1]==10.0){
                Calculation.set(l2,k1,10.0);
                l2++;
            }
            else{
                System.out.println("Nothing to do");
            }
            k2++;
            l1++;


        }



    }
    Calculation.print(9,1);
}

}

输出:

0.0   40.0    0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     20.0   0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  10.0 0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  40.0       0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0       10.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0

所需的输出:

0.0   40.0    40.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     20.0   0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  10.0  10.0       0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0

这意味着应在同一行分配所有相同数量的标记。所有标记40.0分配在第一行。所有标记10.0都分配在第三行,等等。

我如何迭代循环以实现这一目标?

提前谢谢

0 个答案:

没有答案