在C ++中将Matlab矩阵转换为数组的有效方法 [英] Efficient way of converting matlab matrix to array in c++
问题描述
我有一个Matlab代码,应该将其转换为c ++. 在一个文件中,有很多矩阵,我想将它们转换为数组(或向量).我需要转换这些矩阵的有效方法.
I have a Matlab code which I should convert to 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.
谢谢
推荐答案
如果该值是常量(例如,每次您想更改该值,您很高兴重新编译),则可以执行以下操作:
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).
如果值正在更改,那么您想使用vector<double> f;
,稍微清理一下输入内容,然后使用类似以下内容:
If the values are changing, then you want to use a vector<double> f;
, clean up the input a bit and use something like:
ifstream infile("numbers.txt");
while(infile >> value)
{
f.push_back(value);
}
这篇关于在C ++中将Matlab矩阵转换为数组的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!