# vector of matrix

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 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)

( 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++)
v.push_back(x);


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

I have a doubt Is my answer good?

( 2015-07-09 04:05:26 -0500 )edit
1

@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)

( 2015-07-09 04:12:42 -0500 )edit
2

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

( 2015-07-09 04:29:04 -0500 )edit
1

thanks all :)

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

Sort by ยป oldest newest most voted
vector<Mat> v;
for (int i=0;i<10;i++)
v.push_back(Mat::zeros(5,1,CV_32F));

more

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 589 times

Last updated: Jul 09 '15