0

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
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Saswati
  • 192
  • 8
  • If you need to set the matrix values using specific values, rather than calculations based on the the single index `i` then I don't think a for-loop is appropriate. Why do you need to do this with a for-loop? – Bobulous Aug 10 '18 at 19:06
  • Why would you expect calls `set(..., 1)` to ever set a value other than `1`? If you expect some cell to have value `2`, don't you think you need some other call with value 2 somewhere? --- And what does any of this have to do with *"How to print ..." in the title? – Andreas Aug 10 '18 at 20:10

4 Answers4

1

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);
}
}
agillgilla
  • 859
  • 1
  • 7
  • 22
1

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);
}
}
Goutham
  • 307
  • 2
  • 11
0
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);
    }
}
JohanLarsson
  • 195
  • 1
  • 7
0
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);
  }
}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59