javaarrayssortingmatrixjama

Jama matrix and Logical error in if else statement in java


I try to implement some code block.I have four array.

    double[]timea={1.0,2.0,3.0,4.0,5.0,6.0};
    double[]speed={11.0,12.0,8.0,13.0,9.0,6.0};
    double[]timeb={1.5,2.5,4.0,5.5};
    double[]speedb={12.3,8.5,6.9,7.8};

1st array define some time say time stamp and some relative speed corresponding each time block.

Like in time 1.0 speed is 11.0,in time 2.0 speed is 12.0,in time 3.0 speed is 8.0.... etc.

3rd array define timestamp b with some corresponding speed. Like at time 1.5 speed is 12.3, at time 2.5 speed is 3.8, at time 4.0 speed is 5.6 ...etc

I want to write down a program which merge those time and there speed with respect to time.

So the desire output will be

1.0 11.0
1.5 12.3
2.0 12.0
2.5 8.5
3.0 8.0
4.0 13.0
4.0  6.9
5.0  9.0
5.5  7.8
6.0  6.0

I write down a code for that

public class Check {
public static void main(String args[]){
    Matrix abc=new Matrix(10,2);
    double[]timea={1.0,2.0,3.0,4.0,5.0,6.0};
    double[]speed={11.0,12.0,8.0,13.0,9.0,6.0};
    double[]timeb={1.5,2.5,4.0,5.5};
    double[]speedb={12.3,8.5,6.9,7.8};
    int k=0,k1=0;
    while(k<timea.length){

            abc.set(k, 0, timea[k]);
            abc.set(k, 1, speed[k]);

        if(timea[k]<timeb[k1]){
            abc.set(k,0,timeb[k1]);
            abc.set(k,1,speedb[k1]);
            if(k1<timeb.length-1){
                k1++;
            }
        }
        else if(timea[k]>timeb[k1]){
            abc.set(k,0,timea[k]);
            abc.set(k,1,speed[k]);
        }
        k++;
    }
    abc.print(3,6);
}

}

Program output:

1.500000 12.300000
2.500000 8.500000
4.000000 6.900000
5.500000 7.800000
5.500000 7.800000
6.000000 6.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000

So please help me to find out the logical error.


Solution

  • I think the easiest way to do this is just to dump everything into a single ArrayList and then just call sort(Comparator) to sort the data by time.

    After the data is sorted, add it to the matrix.

    The only real trick is to declare a class to hold the pairs of data so that they can be easily sorted. After you have that it's literally one line of code to sort them.

    public class PairwiseSort {
    
       public static void main( String[] args ) {
          double[] timea = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
          double[] speed = {11.0, 12.0, 8.0, 13.0, 9.0, 6.0};
          double[] timeb = {1.5, 2.5, 4.0, 5.5};
          double[] speedb = {12.3, 8.5, 6.9, 7.8};
    
          ArrayList<Datum> data = new ArrayList<>();
          for( int i = 0; i < speed.length; i++ ) {
             data.add( new Datum( timea[i], speed[i] ) );
          }
          for( int i = 0; i < speedb.length; i++ ) {
             data.add( new Datum( timeb[i], speedb[i] ) );
          }
    
          data.sort( Comparator.comparing( Datum::getTime ) );
          System.out.println( data );
    
          Matrix abc=new Matrix(10,2);
          for( int i = 0; i < data.size(); i++ ) {
             Datum datum = data.get( i );
             abc.set( i, 0, datum.getTime() );
             abc.set( i, 1, datum.getSpeed() );
    
          }
    
       }
    }
    
    class Datum {
    
       double time;
       double speed;
    
       public Datum( double time, double speed ) {
          this.time = time;
          this.speed = speed;
       }
    
       public double getTime() {
          return time;
       }
    
       public void setTime( double time ) {
          this.time = time;
       }
    
       public double getSpeed() {
          return speed;
       }
    
       public void setSpeed( double speed ) {
          this.speed = speed;
       }
    
       @Override
       public String toString() {
          return "Datum{" + "time=" + time + ", speed=" + speed + '}';
       }
    
    }
    
    class Matrix {
       Matrix( int a, int b ) {}
       void set( int a, int b, double x ) {}
    }