I want to know the conditions for the 4 main areas of a matrix besides the diagonals.
For example in the following matrix
A=[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;]
The elements from the north side would be 2,3,4,8; from the west side would be 6,11,16,12; from the east side 10,15,20,14; and from the south side 22,23,24,18;
All I figured out is that the north part can be written as:
for(i=0;i<n;i++)
for(j=i+1;j<n-i-1;j++)
printf("%d",v[i][j])
For the other areas, I'm stuck. Can anyone help me?
Suppose we interpret the first index, i
, as a row index, and the second, j
, as a column index. This is consistent with the usual index order in mathematical notation. C in no way requires such an interpretation, but we must choose a convention by which to interpret the compass-direction region labels, and that one is pretty natural.
The indices of elements v[i][j]
on the main diagonal satisfy i == j
. Moving up the matrix (to smaller row numbers) reduces i
, so matrix elements above the main diagonal satisfy i < j
. Similarly, elements below the main diagonal satisfy i > j
.
The indices of elements on the secondary diagonal satisfy i == n - 1 - j
(assuming zero-based indexing). Again, moving up decreases i
and moving down increases it, so elements above this diagonal satisfy i < n - 1 - j
, and those below it satisfy i > n - 1 - j
.
Each of the four regions of interest is characterized uniquely by whether it is above or below those diagonals, so the index conditions for each region are a combination of the corresponding conditions for being above or below the diagonals. For example, the west region is below the main diagonal and above the secondary, so it follows that its index conditions are i > j && i < n - 1 - j
. The index conditions for the other regions can be determined analogously.