extract diagonal elements from a matrix

Hi,

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.

This is the code:

#include <conio.h>
#include<iostream>
#include<cmath>
#include <complex>
#include <string>
#include <iomanip>
using namespace std;

//Main function
//All the operations is done here
int main(int argc, char** argv)
{
int order = 4;
int i, j;
complex <double> W[4][4];

complex<double> Z[4][4] = { {1, 2,3,4},{4,3,4,5},{2,3,2,1},{3,4,5,2} };

//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];

}

else
W[i][j] = 0;
std::cout << W[i][j] << ' ';
}

std::cout << std::endl;

}

system("pause");
return 0;
}


Thank you.

Last edited on
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.

#include <conio.h>
#include<iostream>
#include<cmath>
#include <complex>
#include <string>
#include <iomanip>
using namespace std;

//Main function
//All the operations is done here
int main(int argc, char** argv)
{
int order = 4;
int i, j;
//complex <double> average1= (0.0, 0.0);
//complex <double> average2 = (0.0, 0.0);
//complex <double> d1sum = (0.0,0.0);
//complex <double> d2sum = (0.0, 0.0);
complex <double> W[4][4];

complex<double> Z[4][4] = { {2,3, 4, 5},{1, 2, 3, 4},{0, 1, 3,3},{1, 2 ,3,4} };

//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];
}

if ((i = 0)&& (i < j - 1)&&( i++)) {

W[i][j] = Z[i][i + 1];
}


else
W[i][j] = 0;
std::cout << W[i][j] << ' ';
}

std::cout << std::endl;

}

system("pause");
return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <complex>
using namespace std;


const int order = 4;


void print( complex<double> M[order][order] )
{
   for ( int i = 0; i < order; i++ )
   {
      for ( int j = 0; j < order; j++ ) cout << M[i][j] << '\t';
      cout << '\n';
   }
}


int main()
{
   complex<double> Z[order][order] = { { 1, 2, 3, 4 },{ 4, 3, 4, 5 },{ 2, 3, 2, 1 }, { 3, 4, 5, 2 } };
   complex<double> W[order][order] = { 0 };

   for ( int i = 0; i < order; i++ )
   {
      W[i][i] = Z[i][i];
//    if ( i > 0         ) W[i][i-1] = Z[i][i-1];   // did you want this as well?
      if ( i < order - 1 ) W[i][i+1] = Z[i][i+1];
   }

   cout << "Original matrix:\n";
   print( Z );

   cout << "\nReduced matrix:\n";
   print( W );
}


Original matrix:
(1,0)	(2,0)	(3,0)	(4,0)	
(4,0)	(3,0)	(4,0)	(5,0)	
(2,0)	(3,0)	(2,0)	(1,0)	
(3,0)	(4,0)	(5,0)	(2,0)	

Reduced matrix:
(1,0)	(2,0)	(0,0)	(0,0)	
(0,0)	(3,0)	(4,0)	(0,0)	
(0,0)	(0,0)	(2,0)	(1,0)	
(0,0)	(0,0)	(0,0)	(2,0)	
Last edited on
Thank you for your help.
Topic archived. No new replies allowed.