Trying to resize my matrix


thirdmonkey
08-19-2003, 05:05 PM
I looked at all the messages with "Mat" in them. I did not find an answer to my problem. I hope I don't have to use Kevin Dolan's Matrix classes.

What I am trying to do is to call my function with a matrix, and have it remove a row from the matrix. For example:

Starting with 10 rows...
Mat_DP NDotMatrix(10,3)

then if I want to remove the "6th" row,

NDotMatrix[5][0] = NDotMatrix[9][0];
NDotMatrix[5][1] = NDotMatrix[9][1];
NDotMatrix[5][2] = NDotMatrix[9][2];

NDotMatrix.nrows = 9;

Now I have 9 rows, and the bad row is gone. My order is messed up, but I don't care.

Will this work?
Am I causing a memory leak?

I could not be first guy who wants to shrink the size of his matrix without copying it to a new one.

Thanks in Advance,
TM

Kevin Dolan
08-21-2003, 08:48 AM
That won't work, because the nrows member function just returns the number of rows. It will not allow you to change it.

Likewise, the actual value that holds the number of rows is private, and will therefore not allow you to modify it.

Kevin Dolan

Kevin Dolan
08-21-2003, 08:59 AM
There is a workaround you could try, but it is rather dirty, and not what would normally be considered "good object oriented programming".

You can send the matrix by reference to your function, and then take the address of the matrix. You can then cast that pointer to a simple struct type defined like this:


struct foo {
int nn;
int mm;
double **p;
};


This will let you get around the "privateness" of nn. You can then change the value of nn manually, and of course manuipulate the data through the pointer p.

This is very risky though, for two reasons.

1) This will cause a memory leak if you reduce nn, and possibly segmentation faults if you increase it. You will need another function to "fix" the matrix back to its normal size before it is destroyed.

2) If you use this function with any other matrix class, it will not work at all.

Kevin Dolan

thirdmonkey
08-22-2003, 09:07 AM
Thanks Kevin.

Of course I would change the size back to the original when I was done.

It is risky however for reasoned you talked about. Perhaps I will pass around a new variable which is the matrix's new size...

TM