how to pick out the diagonal elements of 5*5 matrix

--

compare the matrix with its index value matrix and figure out the index values of respective diagonal elements needed

  • in the first diagonal i and j values are same (i==j)
  • in the second diagonal if you closely observe , you can find … j=matrix.length - i - 1

generally we loop through this matrix rows and columns using for loop , now if you add these conditions , the requisite elements can be extracted.

Pseudocode:

Matrix=[4x4];

for loop(i ……..){

for loop(j……..){

  • /include the conditions\*

if(condition 1 for diagonal 1)

push the matrix[i][j] in to an array

if(condition 2 for diagonal 2)

push the matrix[i][j] in to an another array

}

}

the end

--

--

No responses yet