I would like to extract elements from the main diagonal and a diagonal line that is 1-step after the main diagonal and 1-step behind the main diagonal of matrix Z. For example, I have matrix Z=[1 2 3 4; 4 3 4 5; 2 3 2 1; 3 4 5 2], then, I would like to have matrix W=[ 1 2 0 0; 0 3 4 0; 0 0 2 1; 0 0 0 2].Assume that the example is a complex matrix.
//printing out the original matrix read from text file
std::cout << "This is the original matrix:" << "\n";
for (int i = 0; i < order; i++)
{
for (int j = 0; j < order; j++)
{
std::cout << Z[i][j] << ' ';
}
std::cout << std::endl;
}
std::cout << "This is a new matrix:" << "\n";
for (i = 0; i < order; i++)
{
for (j = 0; j < order; j++)
{
if (i == j) {
W[i][j] = Z[i][j];
}
*I have no idea how to set the condition
if ((i=0) && (j = i+1) &&(j<order)) {
W[i][j] = Z[i][j];
for(x = 0; x< columns; x++)
matrix[x][x]; //is main diagonal
for(x = 0; x< columns-1; x++)
matrix[x][x+1]//is shifted one column over... but you are down by 1 column here, right (see loop cond)
Sorry, I still cannot get your point especially in my way of writing the code. I tried to write my code like this, which I absolutely not sure it is right or wrong. The fact is, it still does not work.
//printing out the original matrix read from text file
std::cout << "This is the original matrix:" << "\n";
for (int i = 0; i < order; i++)
{
for (int j = 0; j < order; j++)
{
std::cout << Z[i][j] << ' ';
}
std::cout << std::endl;
}
std::cout << "This is a new matrix:" << "\n";
for (i = 0; i < order; i++)
{
for (j = 0; j < order; j++)
{
if ((i = 0)&& (i < j)&&( i++)) {
W[i][j] = Z[i][i];
}