Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you can only push_back elements with the same row-size of the original Mat. also note, that if you pre-allocate your testMat, new elements will get inserted at the bottom (which is probably not what you wanted)

if you wanted an 1d Mat as the outcome, try like this:

// push partial column vecs:
int k = 3, d = 3;
cv::Mat1f testMat;
for(int i=0; i<k;i++){
    cv::Mat1f partial(d,1,i);
    testMat.push_back(partial);
}
std::cout<<testMat.reshape(1,1)<<std::endl; //reshape() to make row-vec
// [0, 0, 0, 1, 1, 1, 2, 2, 2]

if you instead u wanted a 2d Mat, it goes like this:

// push partial row vecs:
cv::Mat1f testMat2;
for(int i=0; i<k;i++){
    cv::Mat1f partial(1,d,i);
    testMat2.push_back(partial);
}
std::cout<<testMat2<<std::endl; 
// [0, 0, 0;
//  1, 1, 1;
//  2, 2, 2]