[R] filling the matrix row by row in the order from lower to larger elements

Carl Witthoft carl at witthoft.com
Fri Apr 6 23:45:42 CEST 2012


Ok, how's this:


Rgames> foo
      [,1] [,2] [,3] [,4]
[1,]    3    6    1   16
[2,]   10   14   12    5
[3,]   11    7   15    9
[4,]    8    4   13    2

Rgames> sapply(1:4,FUN=function(k){ 
foo[k,which(foo==k,arr.ind=T)[2]]<-100;return(foo)})->bar
Rgames> bar
       [,1] [,2] [,3] [,4]
  [1,]    3    3    3    3
  [2,]   10   10   10   10
  [3,]   11   11  100   11
  [4,]    8    8    8    8
  [5,]    6    6    6    6
  [6,]   14   14   14   14
  [7,]    7    7    7    7
  [8,]    4    4    4  100
  [9,]  100    1    1    1
[10,]   12   12   12   12
[11,]   15   15   15   15
[12,]   13   13   13   13
[13,]   16   16   16   16
[14,]    5  100    5    5
[15,]    9    9    9    9
[16,]    2    2    2    2


Rgames> rab<-matrix(apply(bar,1,max),4,4)
Rgames> rab
      [,1] [,2] [,3] [,4]
[1,]    3    6  100   16
[2,]   10   14   12  100
[3,]  100    7   15    9
[4,]    8  100   13    2


-- 

Sent from my Cray XK6
"Quidvis recte factum, quamvis humile, praeclarum."



More information about the R-help mailing list