[R] Searching a relativ maximum of a matrix

Chris82 rubenbauar at gmx.de
Sun Sep 21 02:48:03 CEST 2008


Hello R users,

is there a possibility to get the relativ maximum of a matrix?
To get the absolut maximum I use max(matrix)

For example: 

The absolut maximum of this matrix is 6[3,6], but the relativ maximum is
6[3,6] and 4[7,6], because both values are the highest value in comparison
to their eight neighbours values.

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    2    2    2    1    1    2
[2,]    1    2    3    3    3    4    4    3
[3,]    3    3    3    4    4    6    5    4
[4,]    4    4    4    3    3    4    4    3
[5,]    3    4    4    3    3    3    2    2
[6,]    3    3    3    3    3    3    2    2
[7,]    2    2    2    2    2    4    2    1
[8,]    1    1    0    0    0    0    0    0

my first idea was to use this code:

a <- matrix 

for (i in 2:7){
  for (j in 2:7){
relmax <- a[a[i,j] > a[(i-1),(j-1)], a[(i-1),j], a[(i-1),(j+1)], a[i,(j-1)],
a[i,(j+1)], a[(i+1),(j-1)], a[(i+1),j], a[(i+1),(j-1)]]
  }
}

but the code doesn't really work

thanks.

greets
-- 
View this message in context: http://www.nabble.com/Searching-a-relativ-maximum-of-a-matrix-tp19590576p19590576.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list