CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: how to delete rows of a matrix

1. Member
Join Date
Aug 2003
Posts
90

## how to delete rows of a matrix

hi, i have a matrix. now, i want to delete the entire row of this matrix. how can i do it ? may be i need to delete few rows.

can you suggest a good algorithm which can delete few rows from a matrix.

2. Member +
Join Date
Feb 2003
Location
Bilbao
Posts
513
What do you mean with delete? You mean setting to 0?

The simplest and quicker form is a for cycle.

3. Member +
Join Date
Aug 2003
Posts
938
here you go.....i did not test this so might be some mistakes in it:
for(short i =0; i<Numer of rows to delte here;i++)
{
for(short j=0;j<NUmber of elements in a row;j++)
{
matrix[i][j]=whatever you want it to be set here

}
}

4. Member
Join Date
Aug 2003
Posts
90
What do you mean with delete? You mean setting to 0?
no i dont mean that. i mean that row will not be there.

for example , say 3 x 3 matrix

mat = { (1, 2, 3) ,(4, 5, 6),(7 ,8 ,9 ) }

now if i delete 2nd row it will be

mat= { (1 , 2, 3), ( 7 ,8, 9) }

i need to delete several rows . can you tell how can i remove rows from a matrix.

thanks

5. Member
Join Date
Aug 2003
Posts
90
Quell....i think your algorithm will not work . as you see, i have given one example in my previous post what i want actually . i dont want to set or replace rows but i want to delete that rows completely.

6. Member
Join Date
Feb 2004
Posts
45
Well this is going to be ugly:
Code:
```mat = { (1, 2, 3) ,(4, 5, 6),(7 ,8 ,9 ) }

now if i delete 2nd row it will be

mat= { (1 , 2, 3), ( 7 ,8, 9) }```
It wouldn't be such a good idea to try doing that!
First of all you should work a little differently.
Code:
```int **mat = new int*[3];
for (int i = 0; i < 3; ++i)
{
mat[i] = new int[3];
}
// here you should intialize your matrix "mat"
initialize(mat);
// here you can work with the matrix
//..
//..
//..
// now when you want to delete the second row
delete [] mat[1];
int **tmpMat = new int*[2];
tmpMat[0] = mat[0];
tmpMat[1] = mat[2];
delete [] mat;
mat = tmpMat;
// now mat is the requested matrix 2X3 or what ever
// and continue working with mat as the new matrix
// ..
// ..
// ..```
Well I told you it's going to be ugly, and actually I do not recomend doing it that way. You have many ways you can avoid this:
1. Define your own class Matrix which supports such operation.
2. Use stl's vector<int> or actually vector<vector<int> >, you can see in code guru a very interesting discussion about it at: http://www.codeguru.com/forum/showth...hreadid=280989
Last edited by nfireman; March 25th, 2004 at 10:48 AM.

7. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,712
Example using vector ...

Code:
```#include <vector>
#include <iostream>

using namespace std;

typedef std::vector<int> V1D;
typedef std::vector<V1D> V2D;

void PrintVector2D(const V2D & v)
{
for (int i=0; i<v.size(); ++i)
{
for (int j=0; j<v[i].size(); ++j)
{
cout << v[i][j] << " ";
}
cout << endl;
}
}

int main()
{
V2D v(3,V1D(3));  // 3 x 3

// give some values

int ic = 0;

for (int i=0; i<v.size(); ++i)
{
for (int j=0; j<v[i].size(); ++j)
{
v[i][j] = ++ic;
}
}

// print out original matrix

cout << "\n\nThe original matrix is \n\n";
PrintVector2D(v);

// erase a row

v.erase(v.begin()+1); // erase 2nd row

//print out

cout << "\n\nThe matrix after erasing second row is \n\n";
PrintVector2D(v);

return 0;
}```

8. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: how to delete rows of a matrix

Originally posted by TheMummy
hi, i have a matrix. now, i want to delete the entire row of this matrix. how can i do it ? may be i need to delete few rows.

can you suggest a good algorithm which can delete few rows from a matrix.
First, there is no such thing as a "matrix" in C++, so you have to tell us what you mean by "matrix". If you are talking about a multi-dimensional array, arrays cannot change size. There is no such thing as "deleting a row" in an array -- if the array is 3 rows by 3 columns, nothing can make it 2 rows by 3 columns. So therefore you can't use arrays to do what you want.

If you're talking about a dynamically allocated two dimensional array, then the ugly code that nfireman shows you will "delete" a row. If you are using std::vector, then deleting a row is as simple as calling the std::vector::erase() function to delete an entire row.

Regards,

Paul McKenzie

9. Junior Member
Join Date
Mar 2004
Posts
7
As long as you are using C arrray to hold your data (assuming that you do already done that), you can simplay move the memory of the row after the one that you like to delete by simply memcpy them ( you have a comtinuece memory so this will work) then remeber that you have a matrix with a new size. This is like hacking but the only other solution is to use STL valarray and recreting the object which is mach safer but will act slower (valarray only work for numbers!)

10. Member
Join Date
Feb 2004
Posts
45
Well now after I gave you an ugly code and after the sting that I've got from an Elite Member about that
I feel that I owe you something that looks better. I'm not sure if you are familiar with the stl, but be sure that it's worth while learning it!
Anyway this will be a start for you.
The following code is a short program that works on a multi-dimensional vector, it allows you to delete a row from the "Matrix":
Code:
```#include <iostream>
#include <vector>

using namespace std;

void print(const vector<vector<int> > &mat);

int main()
{
vector<vector<int> > mat;
mat.resize(3);
for (int i = 0; i < mat.size(); ++i)
mat[i].resize(3);

for (i = 0; i < mat.size(); ++i)
{
for (int j = 0; j < mat[i].size(); ++j)
{
mat[i][j] = mat.size() * i + j + 1;
}
}
// Here you can work with the "Matrix" (actually a multi-dimensional vector)
//..
//..
//..
// Print it for example
cout << "print the original matrix:" << endl;
print(mat);

// Now I beleive, you want to delete a row
mat.erase(mat.begin() + 1);

// And now you can print it again
cout << "print the matrix after we have deleted the second row" << endl;
print(mat);

return 0;
}

void print(const vector<vector<int> > &mat)
{
for (int i = 0; i < mat.size(); ++i)
{
cout << "{";

for (int j = 0; j < mat[i].size(); ++j)
{
cout << mat[i][j];

if (j != mat[i].size() - 1)
cout << ",";
}

cout << "}" << endl;
}
}```
And this is the output:

print the original matrix:
{1,2,3}
{4,5,6}
{7,8,9}
print the matrix after we have deleted the second row
{1,2,3}
{7,8,9}

Summery: This is a better way to work with a multi-dimensional vector, but I must say that the best way in my opinion is that you will define your own kind of Matrix class that will support all the operation that you desire. You can implemet that class using the vector class of stl, you can even make it a template class so that you can create a Matrix of any type (like that: Matrix<ItemType> mat) just like vector.
Good luck,

Nimrod Fireman

11. Member
Join Date
Aug 2003
Posts
90
hi, fireman....thanks for the ugly code. i have completed and tested that code. its working perfectly

however, i am a C programmer. but i understand C++ codes and also STL although i dont work on them.

if i wanted to delete a column instead of a row then i think it would be difficult to use pointer. i find it interesting to know how to delete a column....i think it wont be easier thing as pointer points to individual rows.

can you suggest some way how can i delete a column(preferably in C).

or is there any reserved function which can delete column in STL (C++)?

12. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,712
A sample STL version (using the same code that
I had previously posted - and without checks that
the column actually exists) ...

Code:
```void EraseColumn(V2D & v , int col)
{
for (int i=0; i<v.size(); ++i)
{
v[i].erase( v[i].begin()+col );
}
}```

13. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449
Originally posted by TheMummy
or is there any reserved function which can delete column in STL (C++)?
Write a loop that removes the first element from each of the vectors that make up the matrix:
Code:
```#include <vector>
#include <iostream>

typedef std::vector<int> Int1D;
typedef std::vector<Int1D> Int2D;

int mat[3][3] = { {1,2,3}, {4,5,6}, {7,8,9}};

using namespace std;

void RemoveColumn(Int2D& Matrix, int n)
{
int nRows = Matrix.size();
for ( int i = 0; i < nRows; ++i )
{
if ( n < Matrix[i].size() )
Matrix[i].erase(Matrix[i].begin() + n);
}
}

void DisplayMatrix(const Int2D& Matrix)
{
int nRows = Matrix.size();
for ( int i = 0; i < nRows; ++i)
{
int nCols = Matrix[i].size();
cout << "{ ";
for ( int j = 0; j < nCols; ++j)
cout << Matrix[i][j] << " ";
cout << "}" << endl;
}
}

int main()
{
Int2D Matrix(3, Int1D(3));
int i, j;
for ( i = 0; i < 3; ++i )
for ( j = 0; j < 3; ++j )
Matrix[i][j] = mat[i][j];

cout << "Original" << endl;
DisplayMatrix(Matrix);
RemoveColumn(Matrix, 0);
cout << endl << "Changed" << endl;
DisplayMatrix(Matrix);
}```
Regards,

Paul McKenzie
Last edited by Paul McKenzie; March 27th, 2004 at 02:39 PM.

14. Member
Join Date
Aug 2003
Posts
90
here i have a question on Philip Nicoletti' code ( first post).
he has usued

Code:
`void PrintVector2D(const V2D & v)`
why you are using "const" ? ....the matrix is changing ... its not constant at all.

suppose i am writing
Code:
` const int x =5 ;`
any change to alter the value of x is illegal . is not it ? same case is also here.

here we are changing the matrix ( by deleting row/column) so it is not const thing . i am surprised that compiler did not complain at all !!

can you tell why compiler allowed this illegal work ?

rest of the thing is ok.

15. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449
Originally posted by TheMummy
here i have a question on Philip Nicoletti' code ( first post).
he has usued

Code:
`void PrintVector2D(const V2D & v)`
why you are using "const" ? ....the matrix is changing ... its not constant at all.
When you pass a parameter as const, you are stating that the parameter doesn't change within the function. The matrix is not changing within the PrintVector2D function, therefore the parameter is const.

Not only that, it is prudent to declare the parameter as a const reference. If not, you will be incurring excess overhead since the compiler will make a copy of your matrix if you didn't pass it as const. If the function doesn't change the value, pass it as const (reference). As a matter of fact, you can change the code I wrote and pass the vectors as const in the print function.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; March 27th, 2004 at 02:38 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•