Ask Your Question
0

How to save matrix 2d to array

asked 2017-01-27 01:28:53 -0600

Shabrina gravatar image

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 flag offensive close merge delete

Comments

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);
LBerger gravatar imageLBerger ( 2017-01-27 01:36:35 -0600 )edit

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

berak gravatar imageberak ( 2017-01-27 01:39:00 -0600 )edit

yes, I wan to merge this 2 matrices

Shabrina gravatar imageShabrina ( 2017-01-27 03:05:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-01-27 01:49:35 -0600

berak gravatar image

updated 2017-01-27 01:51:37 -0600

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]
edit flag offensive delete link more

Comments

thank you, for your answer. That's very helpful

Shabrina gravatar imageShabrina ( 2017-01-27 03:18:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-27 01:28:53 -0600

Seen: 328 times

Last updated: Jan 27 '17