I need to write a generalizable code to compare numbers in each row of a square matrix. Let's assume a 3x3 matrix
a11 a12 a13
a21 a22 a23
a31 a32 a33
I want to write a code to check if the succeeding number in each row is greater than or equal to the preceding number.
So for row 1, there are three comparisons: a12 >= a11, a13 >= a12 and a13>=a11
For row 2 also there are three comparisons: a22 >= a21, a23 >= a22 and a23>=a21
and three comparisons to be made for row 3: a32 >= a31, a33 >= a32 and a33>=a31
So a total of 9 comparisons for a 3x3 matrix. I need to store the comparison result as a vector that will have 9 values for each comparison: taking the value 1 if the succeeding number in each row is greater than or equal to the preceding number and taking the value 0 if the succeeding number in each row is less than the preceding number. I know that I first need to start with creating a vector (let's say z) that has 9 elements (each set to 0 initially):
z<-rep(0,9)
This would be followed by a loop on the rows of the matrix to enable the above comparisons and replace 0's with 1 in the z vector when the >= condition is satisfied. Any help in writing this loop would be much appreciated! Thanks.