使用类似Matlab的样式初始化ejml中的向量

时间:2015-05-15 08:27:55

标签: java matlab ejml

在Mablab / Octave中,我可以创建一个具有范围风格的新矢量,例如:
v = [1:10];

然而,当我把它放入ejml等式(Java)时,它不起作用:
eq.process("v = [1:10]");

它仅适用于显式初始化,如:
eq.process("v = [1 2 ]");

任何人都可以这样做,或者建议我解决这个问题吗? 感谢。

2 个答案:

答案 0 :(得分:1)

其实这段代码

eq.process("A = [1:5]");
DMatrixRMaj A = new DMatrixRMaj(eq.lookupMatrix("A"));

回馈

Type = dense64 real , numRows = 1 , numCols = 5
 1,000  2,000  3,000  4,000  5,000 

答案 1 :(得分:0)

要使用矩阵和向量,你可以使用普通的java运算符和循环它这里是一个解决线性系统的例子,添加元素然后设置一个范围向量列和行

我希望这有助于你

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);



    }

}

结果:

输入数字: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