1
    c1  c2  C3  C4                          c1  c2  C3  C4
 R1 1   0   0   0                        R1 1+1 -1  0   0
 R2 0   0   0   0                        R2 -1  1   0   0
 R3 0   0   0   0                        R3 0   0   0   0
 R4 0   0   0   0                        R4 0   0   0   0


    c1  c2  C3  C4                       c1 c2  C3  C4
R1  2   -1  0   0                 R1     2  -1  0   0
R2  -1  1+1 -1  0                 R2    -1  2   -1  0
R3  0   -1  1   0                 R3    0   -1  1+1 -1
R4  0   0   0   0                 R4    0   0   -1  1


    c1  c2  C3  C4
 R1 2   -1  0   0
R2  -1  2   -1  0
R3  0   -1  2   -1
R4  0   0   -1  1

So, this is my problem.I have to generate this kind of matrix step by step. The steps are shown here. After searching on google I came to know that this kind of matrix is called symmetric matrix. The matrix and transpose of this matrix is same. I try to write down some code to implement this.

 import Jama.Matrix;

public class mutest {
public static void main(String args[]){
    Matrix omega=new Matrix(5,5);
    Matrix omega1=new Matrix(5,5);
    for(int i=0;i<=4;i++){
        for(int j=0;j<4;j++) {

                if (i == j) {
                    omega1.set(i, j, 1);//Here I set the value 1 if i=j
                } else {
                    omega1.set(i, j, -1);//Here I set the value -1 if i not equal j
                }

        }


        omega=omega.plus(omega1);
        omega.print(9,6);


        }

    }


}

But I don't get the desire output: I got

 5.000000  -5.000000  -5.000000  -5.000000   0.000000
-4.000000   4.000000  -4.000000  -4.000000   0.000000
-3.000000  -3.000000   3.000000  -3.000000   0.000000
-2.000000  -2.000000  -2.000000   2.000000   0.000000
-1.000000  -1.000000  -1.000000  -1.000000   0.000000

This one is not symmetric matrix. Can anyone tell me what should I change in my code block. I almost reach the goal.

Encipher
  • 1,370
  • 1
  • 14
  • 31
  • 1
    *"I have to generate this kind of matrix step by step"* But your code is not following the steps outlines in the beginning of the question. Do you even understand what those steps are? – Andreas May 30 '18 at 21:15

3 Answers3

2

You seem to have an off-by-one error.

for(int i=0;i<=4;i++){
    for(int j=0;j<4;j++) {

i includes 4, but j only loops up to 3.

Erik Levin
  • 47
  • 1
  • 4
1
omega=omega.plus(omega1);

Is in the wrong place; you're adding a partially updated copy of omega1 to omega every time you complete a row. So the first row gets added 5 times, the second row 4 times... Combine that with not ever updating the last column, and that explains your result.

DavidW
  • 1,413
  • 10
  • 17
1

Not an answer but just a clarification. Sorry, but your logic is not complete. Consider the first traverse, i = 0; and then j will iterate from 0 to 3. So you as per your code, you are setting:

R1C1 = 1; 
R1C2 = -1;
R1C3 = -1;
R1C4 = -1; 

What if

 if (i == j) {
        omega1.set(i, j, 1);//Here I set the value 1 if i=j
        if(i < omega1.length ) { //Whatever the method is to find the length, in this example 5.  
          omega1.set(i+1, j , -1);
          omega1.set(i, j+1 , -1);
           break; //from the second loop.
        }
      } 

Just a quick solution with your approach in mind, you can always optimize it or make it better.

Note: Have never used Jama.Matrix so don't know the functions but I believe basically its a 2D array with added mathematical functions. Jeez I sound silly !!

  • Can you write this code using 2D array? That will be ok for me. – Encipher May 31 '18 at 06:22
  • @EllenaMori No, I don't think anyone should provide you a readymade solution, the learning curve needs to be scaled by you alone. I have provided you with the zest of the logic, you should be able to wrap it up and have a working solution in very short time. Think about it, if I give you a readymade solution, what will be your learning in it ? Good luck !! –  May 31 '18 at 06:46