2012-02-14 7 views
3

私がのstd ::ベクトルを持っていると私は私がやったARMA :: rowvec ::ベクトル<int>

ARMA :: rowvecに変換したい:

vector<int> x = foo(); 
rowvec a; 

vector<int>::const_iterator iter2; 
int j = 0; 
for(iter2 = x.begin(); iter2 != x.end(); ++iter2) { 
    a(j++,0) = *iter2; 
} 
a.print("a"); 

を私取得:

error: Mat::operator(): out of bounds 

terminate called after throwing an instance of 'std::logic_error' 
    what(): 

代わりa(j++,0) = *iter2;の私は最終rowvecでa << *iter2;を使用し、私は最後の要素を取得する場合。あなたが使用している場合

答えて

0

ARMA :: rowvec、ARMA ::マット、文字列(定数、char *を読んで)、または初期化子リストがかかりますArmadillo Documentation rowvecのコンストラクタを1として

vector<int> x = foo(); 
vector<int>::const_iterator iter2; 
stringstream buffer; 
for(iter2 = x.begin(); iter2 != x.end(); ++iter2) 
{ 
    buffer << *iter << " ";  
} 
// To remove the extra trailing space, not sure if it is needed or not 
string elements = buffer.str().substr(0,buffer.str().length()-1); 

rowvec a = rowvec(elements.c_str()); 

のようなものを試してみてくださいC++ 11。

2

aux_memポインタを取るコンストラクタを使用していますか?

rowvec a(x.pointer, x.size()); 
+0

これは私が言う。「STDの不正な使用を::ベクトル> ::ポインタ」 – nkint

6

行ベクトルのサイズを設定するのを忘れました。

より正確なコードは次のようになります

vector<int> x = foo(); 
rowvec a(x.size()); 
... rest of your code ... 

これは、STDを変換することも可能です:: conv_to機能を介してアルマジロ行列またはベクトルのベクトル。だからではなく、手動でループを行うので、あなたがこれを行うことができます:

vector<int> x = foo(); 
rowvec a = conv_to<rowvec>::from(x); 

rowvec行<ダブル>の同義語であること。 Rowクラスのドキュメントを参照してください。そのため、両方のコード例では、intdouble変換が発生しています。それが望ましくない場合は、代わりにirowvecを使用してください。

6

最近のバージョンのArmadilloでは、std :: vectorのインスタンスから直接行列/ベクトルオブジェクトを構築できます。例えば

std::vector<double> X(5); 

// ... process X ... 

arma::vec Y(X); 
arma::mat M(X); 
関連する問題