Search code examples
javamatlabejml

Initialize a vector in ejml with Matlab-like style


In Mablab/Octave, I can create a new vector with range-style, for example:
v = [1:10];

However, when I put it into ejml equation (Java), it doesn't work:
eq.process("v = [1:10]");

It only works for explicit initialization like:
eq.process("v = [1 2 ]");

Can anyone do that, or suggest me a workaround for it? Thanks.


Solution

  • To work with matrix and vectors you can use normal java operators and loops ans det it here is a example solving a linear system, and adding elements and then setting a Range vector column and row

    I hope this helps you

    import org.ejml.factory.SingularMatrixException;
    import org.ejml.simple.SimpleMatrix;
    
    /**
     * Created by anquegi on 15/05/15.
     */
    public class TestEjml {
    
    
    
        public static void main(String args[]){
    
            //Solving a system
            SimpleMatrix A = new SimpleMatrix(2,2);
            SimpleMatrix b = new SimpleMatrix(2,1);
            SimpleMatrix x;
    
    
            // Can assign values the usual way
    
            A.set(0,0,1);
            A.set(0,1,4);
            A.set(1,0,1);
            A.set(1,1,1);
    
            b.set(0,0,28);
            b.set(1,0,10);
    
    
            try {
                x = A.solve(b);
                System.out.println(x);
            } catch ( SingularMatrixException e ) {
                e.printStackTrace();
            }
    
            // So to do a Range
            SimpleMatrix my_range_v = new SimpleMatrix(10,1);
    
            for (int i = 0; i < my_range_v.numRows(); i++) {
                my_range_v.set(i,i); // you can set also wit set(row,col,value)
            }
    
            // So to do a Range
            SimpleMatrix my_range_h = new SimpleMatrix(1,10);
    
    
    
            for (int i = 0; i < my_range_h.numCols(); i++) {
                my_range_h.set(i,i); // you can set also wit set(row,col,value)
            }
    
            System.out.println(my_range_v);
            System.out.println(my_range_h);
    
    
    
        }
    
    }
    

    with the result:

    Enter number: 2

    [info] Running ejml.TestEjml 
    Type = dense real , numRows = 2 , numCols = 1
     4,000  
     6,000  
    
    Type = dense real , numRows = 10 , numCols = 1
     0,000  
     1,000  
     2,000  
     3,000  
     4,000  
     5,000  
     6,000  
     7,000  
     8,000  
     9,000  
    
    Type = dense real , numRows = 1 , numCols = 10
     0,000   1,000   2,000   3,000   4,000   5,000   6,000   7,000   8,000   9,000  
    
    [success] Total time: 2 s, completed 15/05/2015 12:16:20