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

Declaring a dynamic matrix

Tags: None
(comma "," separated)
jackeblagare
Registered Member
Posts
6
Karma
0
OS

Declaring a dynamic matrix

Mon Jan 24, 2011 2:59 am
I want to use a matrix to store grayscale values of multiple images. As such, my plan is to have a matrix with the dimension of axb where a = number of values in the vector where I temporarily store the grayscale values per image, b = number of images.

When I use MatrixXf mat(a,b), I get an Assertion failed error upon running the program. Can you anyone give me an insight as to how I may get around to declare it properly?

This is the error:

Code: Select all
pca: lib/eigen/Eigen/src/Core/Coeffs.h:96: typename Eigen::ei_traits<T>::Scalar& Eigen::MatrixBase<Derived>::operator()(int, int) [with Derived = Eigen::Matrix<int, 10000, 10000, 2, 10000, 10000>]: Assertion `row >= 0 && row < rows() && col >= 0 && col < cols()' failed.
Aborted
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Declaring a dynamic matrix

Mon Jan 24, 2011 1:46 pm
hm,

MatrixXf mat(a,b);

gives you a a x b matrix. The error you get means you tried to access mat(i,j) with i and/or j outside the ranges [0:a[, [0:b[. Without more information I cannot help you more.
jackeblagare
Registered Member
Posts
6
Karma
0
OS

Re: Declaring a dynamic matrix

Tue Jan 25, 2011 1:22 am
Here is my code for accessing the matrix:

Code: Select all
void storeVector(){   
   int ctr=0;
   
   mat.resize(gScale.length(),mSize);

   for (int j=mSize; j<mSize+1; j++){   //cols
      for (int i=1; i<gScale.length(); i++){//rows
           mat(i,j) = gScale[ctr];
                ctr++;
        }
   }
    mSize++;
    gScale.empty(); // clear the arraylist
}



mSize is a global variable that keeps track of the number of rows. It is initially set at 1. The program still returns an Assert failure.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Declaring a dynamic matrix

Tue Jan 25, 2011 8:09 am
Indices go from (0,0) to (rows-1,columns-1)...
jackeblagare
Registered Member
Posts
6
Karma
0
OS

Re: Declaring a dynamic matrix

Tue Jan 25, 2011 10:31 am
I realised my mistake. Thank you very much.

One other thing, how do I dynamically-resize the matrix without overwriting the contents of the previous matrix?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Declaring a dynamic matrix  Topic is solved

Tue Jan 25, 2011 10:21 pm
mat.conservativeResize(rows,cols);
jackeblagare
Registered Member
Posts
6
Karma
0
OS

Re: Declaring a dynamic matrix

Wed Jan 26, 2011 5:04 am
Ok. Thanks. It's now working as it should.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft