简体   繁体   English

在c ++中将matlab矩阵转换为数组的有效方法

[英]Efficient way of converting matlab matrix to array in c++

I have a Matlab code which I should convert to c++. 我有一个Matlab代码,我应该转换为c ++。 In one file there are a lot of matrices and I want to convert them to arrays(or vectors). 在一个文件中有很多矩阵,我想将它们转换为数组(或向量)。 I need efficient way of converting those matrices. 我需要有效的方法来转换这些矩阵。

f = [   -.000212080863  .000358589677   .002178236305   ...
        -.004159358782  -.010131117538  .023408156762   ...
        .028168029062   -.091920010549  -.052043163216  ...
        .421566206729   .774289603740   .437991626228   ...
        -.062035963906  -.105574208706  .041289208741   ...
        .032683574283   -.019761779012  -.009164231153  ...
        .006764185419   .002433373209   -.001662863769  ...
        -.000638131296  .000302259520   .000140541149   ...
        -.000041340484  -.000021315014  .000003734597   ...
        .000002063806   -.000000167408  -.000000095158  ];

I tried things like this but all my trials give some errors. 我尝试过这样的事情,但我的所有试验都给出了一些错误。

int* first;
first = new int[5];
first = {1,2,3,4,5};

Note: I can put commas and change [ to { manually. 注意:我可以输入逗号并更改[ to { manual。

Thanks, 谢谢,

If the value is constant (as in, you are happy to recompile for each time you want to change the values), then you can do: 如果值是常量(例如,您很乐意每次想要更改值时重新编译),那么您可以执行以下操作:

double f[] = {  -.000212080863,  .000358589677,   .002178236305,   ... };

(Note the addition of commas, and curly brackets instead of square ones). (注意添加逗号和大括号而不是方括号)。

If the values are changing, then you want to use a vector<double> f; 如果值正在改变,那么你想使用vector<double> f; , clean up the input a bit and use something like: ,清理一下输入并使用类似的东西:

ifstream infile("numbers.txt"); 
while(infile >> value) 
{ 
   f.push_back(value); 
}

You can use something like this: 你可以使用这样的东西:

PMatrix MatrixData::factory(string parser){
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

    // Verifica formattazione matrice

    if (!((parser[0]=='{' && parser[parser.size()-1] == '}')||(parser[0]=='[' && parser[parser.size()-1] == ']')))
        assert( (std::cout<<"Wrong matrix structure"<<std::endl, 0) );

     // Verifica struttura matrice

    boost::char_separator<char> row_sep("[]{};");
    boost::char_separator<char> col_sep(",");
    unsigned int row_number,col_number;

    tokenizer::iterator rowtok_iter;
    tokenizer::iterator coltok_iter;

    row_number = 0;
    tokenizer rowtokens(parser, row_sep);
    for (rowtok_iter = rowtokens.begin();rowtok_iter != rowtokens.end(); ++rowtok_iter)
        row_number++;

    col_number = 0;
    tokenizer coltokens(*rowtokens.begin(), col_sep);
    col_number = std::distance(coltokens.begin(),coltokens.end());

    //cout << row_number << " rows and " << col_number << " columns" << endl;

    unsigned int active_row_col_number;
    double* values = new double[col_number*row_number];
    unsigned int i = 0;

    for (rowtok_iter = rowtokens.begin();rowtok_iter != rowtokens.end(); ++rowtok_iter){
        active_row_col_number = 0;
        tokenizer coltokens1(*rowtok_iter, col_sep);
        for (coltok_iter = coltokens1.begin();coltok_iter != coltokens1.end();++coltok_iter){
            active_row_col_number++;
            values[i]=strtod(coltok_iter->c_str(),0);
            i++;
        }   
        if (active_row_col_number!=col_number)
             assert( (std::cout<<"Wrong matrix structure 1"<<std::endl, 0) );
    }   
    PMatrix ret = MatrixData::factory(row_number,col_number,values);
    delete[] values;
    return ret;
}

which directly parses a Matlab-formatted matrix from a string and puts the result into "values". 它直接从字符串中解析Matlab格式的矩阵,并将结果放入“值”。

This is not the cleanest code you can imagine, but it can be cleaned up. 这不是您能想象的最干净的代码,但可以清理它。 It's using boost::tokenizer, as you can see. 你可以看到它正在使用boost :: tokenizer。 Hope it helps. 希望能帮助到你。

If your compiler supports it, you could also use an initializer list 如果您的编译器支持它,您也可以使用初始化列表

vector<double> f{ -.000212080863, .000358589677, ...} // replace elipses with other numerical values

This will give you the advantage that you vector size is not stack limited. 这将为您提供矢量大小不受堆栈限制的优势。 Keep in mind that matlab uses column order for matrices, so you need to take care when converting those. 请记住,matlab使用矩阵的列顺序,因此在转换矩阵时需要注意。

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

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