This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] Problem with adding row to a matrix

Tags: None
(comma "," separated)
Xtremecpp
Registered Member
Posts
5
Karma
0
Hi,

i' ve got a problem with this code, can somebody help me:

possibility1:
Code: Select all
 
MatrixXd mat = MatrixXd::Zero( 10, 10);
mat.row( 10) = vec;

possibility2:
Code: Select all
 
MatrixXd mat = MatrixXd::Zero( 10, 10);
mat.row( 10) = vec.transpose();

.. where vec is a VectorXd with size 10.

Why neither possibility1 nor possibilty2 works?
With the mat.row( 10) i want to place the values of transposed Vector (or Vector).
After that matrix row with index 10 is Zero and not equal the values of vector vec.

Last edited by bjacob on Sat Jan 03, 2009 1:27 pm, edited 1 time in total.


Xtremecpp, proud to be a member of KDE forums since 2008-Dec.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Hi,

note that the indices start from 0, therefore the index of the 10th and last row is 9, not 10, so if you want a 10x10 matrix with the last row equal to vec, write:
Code: Select all
mat.row(9) = vec;

FYI, in your example, you can also do:
Code: Select all
MatrixXd mat(10,10);
mat << MatrixXd::Zero(9,10), vec.transpose();
[hr]
one more thing, perhaps you misunderstood the meaning of "dynamic-size matrix". Here "dynamic" means the size is known at runtime and the matrix can be explicitly resized using the resize() function. However, there is no implicit resizing, and in particular you cannot dynamically add a new row without creating a new, temporary, matrix. If you really need to do that, then here is an example which appends vec to a 10x10 matrix:
Code: Select all
MatrixXd mat = MatrixXd::Zero(10,10);
mat.set( (MatrixXd(11,10) << mat, vec.transpose()).finished() );

Here, I used set() instead of operator=() such that mat is automatically resized, and operator<< and operator, to append mat and vec to a new, temporary matrix which is then copied to mat, and yes, in that example, there is one useless copy (unless the compiler is smart enough to remove it)

Last edited by ggael on Thu Jan 01, 2009 5:12 pm, edited 1 time in total.
Xtremecpp
Registered Member
Posts
5
Karma
0
Thanks,

for your help.

... now i have another Problem:
Code: Select all
int i=0, j=0;
int *row =&i, *col =&j;
MatrixXd test = MatrixXd::Zero(10,10);
double min = 0.;
test = function(test);    //insert values
min = test.minCoeff( row, col);
printf("min=%f",min);


problem is that no matrix element has the value of variable min.
Whats the problem?


Xtremecpp, proud to be a member of KDE forums since 2008-Dec.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
hm.. I don't get it, have you tried with only:
Code: Select all
min = test.minCoeff();

?

Also, have you tried to print out test:
Code: Select all
std::cout << test << std::endl;

?

and can you show us the implementation of function ?
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Xtremecpp wrote:problem is that no matrix element has the value of variable min.
Whats the problem?


That sounds.... impossible. Can you double check your code? Try to isolate a small testcase and post it here? After "min = test.minCoeff( row, col);" returns, min really should be the smallest coefficient in the matrix test.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Xtremecpp
Registered Member
Posts
5
Karma
0
Hi,

sorry the problem was my fault.
When i am searching for the minimum in the matrix i take "1,23448" but in the matrix the comma is represented by decimal point so i can't found the minimum.

Thanks


Xtremecpp, proud to be a member of KDE forums since 2008-Dec.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Ah, ok. Just FYI, when you do stream << matrix to print your matrix, it will honor the locale settings of your stream. So you can get comma-as-decimal-point by setting another locale for your stream with imbue() or you can pass a global locale setting to your app by setting the LANG variable (at least on unix)


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell