How to print non symmetric value while using for loop in Java

80 views Asked by At

I want to implement a matrix using for loop. To create the matrix I used Jama Matrix Package.

Here is my code

import Jama.Matrix;

public class Matrixnonsym {

   public static void main(String args[]) {
       Matrix Mytest=new Matrix(5,5);
       for(int i=0; i<4; i++) {
           Mytest.set(i,i,1);
           Mytest.set(i+1,i,1);
       }
       Mytest.print(9,6);
   }
}

Here is my output:

1.000000   0.000000   0.000000   0.000000   0.000000
1.000000   1.000000   0.000000   0.000000   0.000000
0.000000   1.000000   1.000000   0.000000   0.000000
0.000000   0.000000   1.000000   1.000000   0.000000
0.000000   0.000000   0.000000   1.000000   0.000000

There is no compilation error or runtime error. The difficulty is that how could I make the (0,0) cell value 2? As this matrix constructed using for loop so all value constructed symmetrically. Then how could I make only one cell with different value?

Desire Output:

2.000000   0.000000   0.000000   0.000000   0.000000
1.000000   1.000000   0.000000   0.000000   0.000000
0.000000   1.000000   1.000000   0.000000   0.000000
0.000000   0.000000   1.000000   1.000000   0.000000
0.000000   0.000000   0.000000   1.000000   0.000000
4

There are 4 answers

0
Goutham On BEST ANSWER

You can use a if condition inside the for loop to have a different value for the particular cell.

import Jama.Matrix;

public class Matrixnonsym {
public static void main(String args[]){
     Matrix Mytest=new Matrix(5,5);
     for(int i=0;i<4;i++){
         if(i == 0){
               Mytest.set(i,i,2);
         }
         Mytest.set(i,i,1);
         Mytest.set(i+1,i,1);
      }
    Mytest.print(9,6);
}
}
0
agillgilla On

I've never used Jama before, but from the Javadoc I think you could just do:

import Jama.Matrix;

public class Matrixnonsym {
public static void main(String args[]){
    Matrix Mytest=new Matrix(5,5);
    for(int i=0;i<4;i++){
        Mytest.set(i,i,1);
        Mytest.set(i+1,i,1);
    }
    Mytest.set(0, 0, 2.0) 
    Mytest.print(9,6);
}
}
0
JohanLarsson On
import Jama.Matrix;

public class Matrixnonsym {
    public static void main(String args[]){
    Matrix Mytest=new Matrix(5,5);
        for(int i=0;i<4;i++){
            if (i==0) {
                Mytest.set(i,i,2);
            } else {
                Mytest.set(i,i,1);
            }
            Mytest.set(i+1,i,1);
        }
        Mytest.print(9,6);
    }
}
0
Slava Vedenin On
import Jama.Matrix;

public class Matrixnonsym {
  public static void main(String args[]){
     Matrix Mytest=new Matrix(5,5);

     // first column
     Mytest.set(0,0,2);
     Mytest.set(1,0,1);

     // others columns
     for(int i=1; i<4; i++){
         Mytest.set(i,i,1);
         Mytest.set(i+1,i,1);
     }

     Mytest.print(9,6);
  }
}

Or

import Jama.Matrix;

public class Matrixnonsym {
  public static void main(String args[]){
     Matrix Mytest=new Matrix(5,5);

     for(int i=0; i<4; i++){
         Mytest.set(i, i, i == 0 ? 2 : 1);
         Mytest.set(i+1, i, 1);
     }

     Mytest.print(9,6);
  }
}