Ask Your Question

vector of matrix

asked 2015-07-09 03:04:12 -0500

Nbb gravatar image

updated 2015-09-06 18:36:33 -0500

How do i initialize a vector of Matrix of type float. Each matrix has 5 rows and 1 column initialized to [0,0,0,0,0]

I could only get to vector<mat_<float>> random;

I have no clue how to initialize size of each Mat. Help thanks

edit retag flag offensive close merge delete


if you can live with stack-based Matx types, it could be:

typedef Matx<float, 5, 1> Matx51f;
vector<Matx51f> V(123, Matx51f(0,0,0,0,0)); // 123 preallocated items

V[3](2) = 7; // access 3rd element of 4th Matx

(you can't do that with 'heap' based Mat or Mat_<float>, since the data from the initialization would be shared among all elements)

berak gravatar imageberak ( 2015-07-09 03:29:07 -0500 )edit

@berak you mean that :

Mat x=Mat::zeros(5,1,CV_32F);
vector<Mat> v;
for (int i=0;i<10;i++)

because memory used by v[0] is same that v[1], v[2] push_back just make a field copy of x

I have a doubt Is my answer good?

LBerger gravatar imageLBerger ( 2015-07-09 04:05:26 -0500 )edit

@LBerger, imho, your answer below is the obvious and correct solution.

but yes, if you push_back() shallow copies of the same Mat, they all will point to same data. it's the same problem with: vector<Mat> V(123,Mat(5,1,CV_32F,0.0f)); // all point to same data

(i just wanted to show up an alternative, but thinking twice about it, i'll rather delete it for being too confusing)

berak gravatar imageberak ( 2015-07-09 04:12:42 -0500 )edit

@berak No don't delete I have learn something! After there is many ways to go somewhere.

LBerger gravatar imageLBerger ( 2015-07-09 04:29:04 -0500 )edit

thanks all :)

Nbb gravatar imageNbb ( 2015-07-09 06:56:33 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-07-09 03:21:02 -0500

LBerger gravatar image
vector<Mat> v;
for (int i=0;i<10;i++)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-07-09 03:04:12 -0500

Seen: 279 times

Last updated: Jul 09 '15