用类似 Matlab 的样式在 ejml 中初始化一个向量

Initialize a vector in ejml with Matlab-like style

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

但是,当我将它放入ejml方程(Java)时,它不起作用:
eq.process("v = [1:10]");

它只适用于像这样的显式初始化:
eq.process("v = [1 2 ]");

谁能做到这一点,或者建议我解决这个问题? 谢谢

要使用矩阵和向量,您可以使用普通的 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

实际上是这段代码

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