简体   繁体   English

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

[英]Initialize a vector in ejml with Matlab-like style

In Mablab/Octave, I can create a new vector with range-style, for example: 在Mablab / Octave中,我可以创建具有范围样式的新矢量,例如:
v = [1:10];

However, when I put it into ejml equation (Java), it doesn't work: 但是,当我将其放入ejml公式(Java)时,它不起作用:
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. 谢谢。

Actually this code 其实这段代码

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

gives back 退还

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

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 要使用矩阵和向量,您可以使用常规的Java运算符并循环进行循环,这是解决线性系统,添加元素然后设置范围向量列和行的示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM