Solved

# Diagonal elements

Posted on 2002-06-14

Hi,

I have to set all the diagonal elements of a 2D matrix to a given value (i.e. zero). This is easy if the matrix is square, but I'm having problems with non-square matrices.

This is what I've tried so far:

int N , M; //Number of rows and columns

float* fData = new float [N * M];

float fValue = 0.0f;

float fSlope = (float)M / (float)N;

for (register int i = 0; i < M; i++)

{

int j = (int) floor( i / fSlope + 0.5);

*fData = fValue;

int nOffset = (int)floor (nCols * slope + 0.5) +1;

fData += nOffset;

}

This works fine if the matrix is square but fails in the non-square case

Thanks for your help,

elito