My code:
import org.ujmp.core.Matrix;
import org.ujmp.core.SparseMatrix;
public class part {
public static void main(String args[]) throws Exception{
Matrix Bigomega=Matrix.Factory.zeros(6,6);
Matrix omega = SparseMatrix.Factory.zeros(6, 6);
int []timea={1,2,3,4,5,6};
int [] timeb={3};
int k1=0,k2=0;
while (k1 < timea.length && k2 < timeb.length ) {
if (timea[k1] < timeb[k2]) {
omega.setAsInt(1, k1, k1);
omega.setAsInt(-1, k1, k1 + 1);
omega.setAsInt(-1, k1 + 1, k1);
omega.setAsInt(1, k1 + 1, k1 + 1);
Bigomega = Bigomega.plus(omega);
omega.clear();
k1++;
}
else if (timea[k1] == timeb[k2]){
omega.setAsInt(1, k1, k1);
omega.setAsInt(-1, k1, k1 + 1);
omega.setAsInt(-1,k1+1,k1);
omega.setAsInt(1,k1+1,k1+1);
Bigomega=Bigomega.plus(omega);
omega.clear();
k2++;
}
}
System.out.println(Bigomega);
}
}
Output:
1.0000 -1.0000 0.0000 0.0000 0.0000 0.0000
-1.0000 2.0000 -1.0000 0.0000 0.0000 0.0000
0.0000 -1.0000 2.0000 -1.0000 0.0000 0.0000
0.0000 0.0000 -1.0000 1.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
The output that came that is not desired. The loop was terminated here after getting the timea[k1] == timeb[k2]
. But I want after getting the similar element from timea and timeb the loop continue until all the elements of timea visit. As per my code it visits 1,2,3 from timea array and then discards. And this is very obvious because of while statement while (k1 < timea.length && k2 < timeb.length )
. Because when it get loop counter k2++
k2 increment by 1. So after getting 3 from timeb k2 become 1.So when it get k2<timeb.length
that means 1<1
which is false so the loop is terminated. But in that time {4,5,6}
of timea is not visited. I want after executing else statement if statement executes again for {4,5,6}
.
How can this be achieved?
Desired output:
1.0000 -1.0000 0.0000 0.0000 0.0000 0.0000
-1.0000 2.0000 -1.0000 0.0000 0.0000 0.0000
0.0000 -1.0000 2.0000 -1.0000 0.0000 0.0000
0.0000 0.0000 -1.0000 2.0000 -1.0000 0.0000
0.0000 0.0000 0.0000 -1.0000 2.0000 -1.0000
0.0000 0.0000 0.0000 0.0000 1.0000 -1.0000