Ask Your Question
1

vector of matrix

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

Nbb gravatar image

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

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

Comments

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

LBerger gravatar imageLBerger ( 2015-07-09 04:05:26 -0600 )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)

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

@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 -0600 )edit
1

thanks all :)

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

1 answer

Sort by ยป oldest newest most voted
4

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

LBerger gravatar image
vector<Mat> v;
for (int i=0;i<10;i++)
    v.push_back(Mat::zeros(5,1,CV_32F));
edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 1,595 times

Last updated: Jul 09 '15