william
08-31-2009, 10:34 PM
Hi, I want to delete a column of a matrix by a function.But I don't know how to do it efficiently.
Here is my code.
void comp_kill(int comp){
MatDoub temp(A.nrows(),A.ncols()-1);
for (int i=0;i<A.nrows();i++)
{
for (int j=0;j<comp;j++) temp[i][j]=A[i][j];
for (int j=comp;j<A.ncols()-1;j++) temp[i][j]=A[i][j+1];
}
A.resize(A.nrows(),A.ncols()-1);
for (int i=0;i<A.nrows();i++)
{
for (int j=0;j<A.ncols()-1;j++) A[i][j]=temp[i][j];
}
}
A is the matrix and a member of a struct. comp_kill is a member function. I want to delete a column(whose index is comp) of A by comp_kill. comp is the input.
I think it is not quite efficient because a medium matrix 'temp' must be built. I am wonderring is there any other way out?
Thanks in advance
Here is my code.
void comp_kill(int comp){
MatDoub temp(A.nrows(),A.ncols()-1);
for (int i=0;i<A.nrows();i++)
{
for (int j=0;j<comp;j++) temp[i][j]=A[i][j];
for (int j=comp;j<A.ncols()-1;j++) temp[i][j]=A[i][j+1];
}
A.resize(A.nrows(),A.ncols()-1);
for (int i=0;i<A.nrows();i++)
{
for (int j=0;j<A.ncols()-1;j++) A[i][j]=temp[i][j];
}
}
A is the matrix and a member of a struct. comp_kill is a member function. I want to delete a column(whose index is comp) of A by comp_kill. comp is the input.
I think it is not quite efficient because a medium matrix 'temp' must be built. I am wonderring is there any other way out?
Thanks in advance