javamatrixjama

JAMA setMatrix documentation need to set a submatrix in given matrix


Can anyone tell me about JAMA MATRIX Package how setMatrix work? Please don't suggest me to see the documentation. I search documentation several time but don't get any example how it work. I have code where I want to set a submatrix with desired position using JAMA MATRIX package.

Matrix A= new Matrix(new double[][]{{2.0,3.0,5.0},{1.0,0.0,3.0},{0.0,1.0,1.0}});
A.print(9,6);
Matrix A1= new Matrix(new double[][]{{1.0,2.0,2.0}});
int []A2=new int[]{2};
int []A3=new int[]{2};
A.setMatrix(A2, A3, A1);
A.print(9,6);

I want to add A1 in second row and second column. But fail to add.The two outputs are same. No difference between them.


Solution

  • Here is an example of your problem.

    Code:

    public class M1test {
    public static void main(String args[]){
        Matrix A= new Matrix(new double[][]{{2.0,3.0,5.0},{1.0,0.0,3.0},{0.0,1.0,1.0}});
        A.print(9,6);
        Matrix A1= new Matrix(new double[][]{{1.0,2.0,2.0}});
        A.setMatrix(2,2,0,2,A1);
        A.print(9,6);
    }
    

    }

    Explanation

    SetMatrix actually used to set a submatrix. So here I want to replace the last row of my matrix A with A1. So A1 is a submatrix, which is going to set in A.

    Now as per documentation

    public void setMatrix(int i0,
                      int i1,
                      int j0,
                      int j1,
                      Matrix X)
     Set a submatrix.
     Parameters:
    i0 - Initial row index
    i1 - Final row index
    j0 - Initial column index
    j1 - Final column index
    X - A(i0:i1,j0:j1)
    

    In my code I want to replace the last row. As A is 3*3 matrix so last row index is 2. So Initial row index is 2.Only one row exist in the submatrix A1. So final row index is also 2. Initial column index is 0 and final column index is 2. So I just changed my code as instructed. Hope you will understand it. For more information please follow the link JAMA Matrix

    Output:

    2.000000   3.000000   5.000000
    1.000000   0.000000   3.000000
    0.000000   1.000000   1.000000
    
    
    2.000000   3.000000   5.000000
    1.000000   0.000000   3.000000
    1.000000   2.000000   2.000000