How to define a matrix with 5000 specific elements

asked 2016-09-05 12:04:25 -0500

jason gravatar image

Hello, I need to define a matrix M, M is 5000*2. In matlab, M=[1,45;89,3;....], it's not regular, so I can't define it in a loop. In my code I tried to define it like

Mat M = (Mat_<int>(num, 3) << 994, 323, 0, 4014, 4295, 4395, 4906, 3423, 2792.....);

But when I use printf("number: %f",<double>(2, 1), it gives me 0. So could anyone tell me how to define such a matrix like in matlab M=[1 2 3; 4 5 6;....]

edit retag flag offensive close merge delete



to access a Mat_<int> , you have to use<int>() (obvious, isn't it ?)

"it's not regular,"` -- what do you mean ?

berak gravatar imageberak ( 2016-09-05 12:06:39 -0500 )edit

Thank you. It worker. How stupid I was, forgot to change the data type. When I say"it's not regular", I mean that it's not random number, nor part a a progression, so I can't define it in a loop. I am not a native English speaker. So I may make mistakes. Anyway, thank you very much.

jason gravatar imagejason ( 2016-09-05 12:18:14 -0500 )edit

what about writing it into a file from matlab(say, csv), and reading it back in ? see e.g. here

berak gravatar imageberak ( 2016-09-05 12:26:45 -0500 )edit

Thank you. I will learn how to do it.

jason gravatar imagejason ( 2016-09-05 12:32:20 -0500 )edit