# How to save matrix 2d to array

I have 2 matrix, egnvecx and egnvecy. egnvecx :

[0.52065742, 0.39711261, 0.75578946, 0;
-0.42713234, 0.88765043, -0.17214788, 0;
0, 0, 0, 1;
0.73923874, 0.233192, -0.63178128, 0]


egnvecy :

[1, 0, 0, 0;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1]


Then, I will save this matrix into array it contain x and y. But I dont know how the step is. this the code will be error.

Mat data_pts = Mat(4,4, CV_32F);
for (int i = 0; i < data_pts.rows; ++i)
{
for (int x=0; x<data_pts.cols; x++){
data_pts.at<float>(i,x) = egnvecx.ptr<float>(i,x);
data_pts.at<float>(i,x) = egnvecy.ptr<float>(i,x);
}
}
cout << data_pts;


thank you :))

edit retag close merge delete

Can you help us ? What is error message?

        // data_pts.at<float>(i,x) = egnvecx.ptr<float>(i,x);  this line is not necessary it is same index than next line...
data_pts.at<float>(i,x) = egnvecy.ptr<float>(i,x);

( 2017-01-27 01:36:35 -0500 )edit

do you want to merge your 2 4x4x1 Matrices into a single 4x4x2 one ?

( 2017-01-27 01:39:00 -0500 )edit

yes, I wan to merge this 2 matrices

( 2017-01-27 03:05:50 -0500 )edit

Sort by ยป oldest newest most voted

your current solution overwrites x with the y position, and so only keeps y.

i think, you wanted to merge your arrays (like a zipper):

Mat_<float> x(4,4);
x << 0.52065742, 0.39711261, 0.75578946, 0,
-0.42713234, 0.88765043, -0.17214788, 0,
0, 0, 0, 1,
0.73923874, 0.233192, -0.63178128, 0;

Mat_<float> y(4,4);
y << 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;

Mat xy;
Mat chn[2] = {x,y};

merge(chn,2,xy);

cerr << xy <<endl;

[0.52065742, 1, 0.39711261, 0, 0.75578946, 0, 0, 0;
-0.42713234, 0, 0.88765043, 1, -0.17214788, 0, 0, 0;
0, 0, 0, 0, 0, 1, 1, 0;
0.73923874, 0, 0.233192, 0, -0.63178128, 0, 0, 1]

more

( 2017-01-27 03:18:40 -0500 )edit

Official site

GitHub

Wiki

Documentation