# filling the matrix row by row in the order from lower to larger elements

13 messages
Open this post in threaded view
|
Report Content as Inappropriate

## filling the matrix row by row in the order from lower to larger elements

 Hello, everybody! I have a matrix "input" (see example below) - with all unique entries that are actually unique ranks (i.e., start with 1, no ties). I want to assign a value of 100 to the first row of the column that contains the minimum (i.e., value of 1). Then, I want to assign a value of 100 to the second row of the column that contains the value of 2, etc. The results I am looking for are in "desired.results". My code (below) does what I need. But it's using a loop through all the rows of my matrix and searches for a matrix element every time. My actual matrix is very large. Is there a way to do it more efficiently? Thank you very much for the tips! Dimitri input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) (input) desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100))) (desired.result) result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0))) for(i in 1:nrow(input)){ # i<-1   mymin<-i   mycoords<-which(input==mymin,arr.ind=TRUE)   result[i,mycoords[2]]<-100   input[mycoords]<-max(input) } (result) -- Dimitri Liakhovitski ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 I maybe missing something but this seems like an indexing problem which doesn't require a loop at all. Something like this maybe? (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2)) output <- matrix(0,max(input),2) output[input[,1],1] <- 100 output[input[,2],2] <- 100 output Cheers On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski <[hidden email]> wrote: > Hello, everybody! > > I have a matrix "input" (see example below) - with all unique entries > that are actually unique ranks (i.e., start with 1, no ties). > I want to assign a value of 100 to the first row of the column that > contains the minimum (i.e., value of 1). > Then, I want to assign a value of 100 to the second row of the column > that contains the value of 2, etc. > The results I am looking for are in "desired.results". > My code (below) does what I need. But it's using a loop through all > the rows of my matrix and searches for a matrix element every time. > My actual matrix is very large. Is there a way to do it more efficiently? > Thank you very much for the tips! > Dimitri > > input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) > (input) > desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100))) > (desired.result) > result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0))) > for(i in 1:nrow(input)){ # i<-1 >  mymin<-i >  mycoords<-which(input==mymin,arr.ind=TRUE) >  result[i,mycoords[2]]<-100 >  input[mycoords]<-max(input) > } > (result) > > -- > Dimitri Liakhovitski > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 Hello, > > I maybe missing something but this seems like an indexing problem > which doesn't require a loop at all. > Yes, but with 'order'. # Original example input <- as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8))) (input) desired.result <- as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100))) (desired.result) all.equal(f(input), desired.result) # Two other examples set.seed(123) (x <- matrix(sample(10, 10), ncol=2)) f(x) (y <- matrix(sample(40, 40), ncol=5)) f(y) Hope this helps, Rui Barradas
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by Dimitri Liakhovitski-2 I think the OP wants to fill values in an arbitrarily large matrix. Now, first of all, I'd like to know what his real problem is, since this seems like a very tedious and unproductive matrix to produce.  But in the meantime,  since he also left out important information, let's assume the input matrix is N rows by M columns, and that he wants therefore to end up with N instances of "100", not counting the original value of 100 that is one of his ranking values (a bad BAD move IMHO). Then either loop or lapply over an equation like (I've expanded things more than necessary for clarity result<-inmatrix for (k in 1:N){ foo <- which (inmatrix == k,arr.ind=T) result[k,foo[2]] <-100 } I maybe missing something but this seems like an indexing problem which doesn't require a loop at all. Something like this maybe? (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2)) output <- matrix(0,max(input),2) output[input[,1],1] <- 100 output[input[,2],2] <- 100 output Cheers On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski wrote:  > Hello, everybody!  >  > I have a matrix "input" (see example below) - with all unique entries  > that are actually unique ranks (i.e., start with 1, no ties).  > I want to assign a value of 100 to the first row of the column that  > contains the minimum (i.e., value of 1).  > Then, I want to assign a value of 100 to the second row of the column  > that contains the value of 2, etc.  > The results I am looking for are in "desired.results".  > My code (below) does what I need. But it's using a loop through all  > the rows of my matrix and searches for a matrix element every time.  > My actual matrix is very large. Is there a way to do it more efficiently?  > Thank you very much for the tips!  > Dimitri  >  > input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))  > (input)  > desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))  > (desired.result)  > result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))  > for(i in 1:nrow(input)){ # i<-1  >  mymin<-i  >  mycoords<-which(input==mymin,arr.ind=TRUE)  >  result[i,mycoords[2]]<-100  >  input[mycoords]<-max(input)  > }  > (result)  > -- Sent from my Cray XK6 "Quidvis recte factum, quamvis humile, praeclarum." ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by Dimitri Liakhovitski-2 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." ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by Carl Witthoft Yes, that's correct - my matrix has N rows. Thank you very much, Carl. This works great: input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) result<-input N<-nrow(input) for (k in 1:N){   foo <- which (input == k,arr.ind=T)   result[k,foo[2]] <-100 } result[result !=100]<-0 Dimitri On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft <[hidden email]> wrote: > I think the OP wants to fill values in an arbitrarily large matrix. Now, > first of all, I'd like to know what his real problem is, since this seems > like a very tedious and unproductive matrix to produce.  But in the > meantime,  since he also left out important information, let's assume the > input matrix is N rows by M columns, and that he wants therefore to end up > with N instances of "100", not counting the original value of 100 that is > one of his ranking values (a bad BAD move IMHO). > > Then either loop or lapply over an equation like (I've expanded things more > than necessary for clarity > result<-inmatrix > for (k in 1:N){ > foo <- which (inmatrix == k,arr.ind=T) > result[k,foo[2]] <-100 > > } > > > > I maybe missing something but this seems like an indexing problem > which doesn't require a loop at all. Something like this maybe? > > (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2)) > output <- matrix(0,max(input),2) > output[input[,1],1] <- 100 > output[input[,2],2] <- 100 > output > > Cheers > > > On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski > wrote: >> Hello, everybody! >> >> I have a matrix "input" (see example below) - with all unique entries >> that are actually unique ranks (i.e., start with 1, no ties). >> I want to assign a value of 100 to the first row of the column that >> contains the minimum (i.e., value of 1). >> Then, I want to assign a value of 100 to the second row of the column >> that contains the value of 2, etc. >> The results I am looking for are in "desired.results". >> My code (below) does what I need. But it's using a loop through all >> the rows of my matrix and searches for a matrix element every time. >> My actual matrix is very large. Is there a way to do it more efficiently? >> Thank you very much for the tips! >> Dimitri >> >> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) >> (input) >> desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100))) >> (desired.result) >> result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0))) >> for(i in 1:nrow(input)){ # i<-1 >>  mymin<-i >>  mycoords<-which(input==mymin,arr.ind=TRUE) >>  result[i,mycoords[2]]<-100 >>  input[mycoords]<-max(input) >> } >> (result) >> > -- > > Sent from my Cray XK6 > "Quidvis recte factum, quamvis humile, praeclarum." > > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. -- Dimitri Liakhovitski marketfusionanalytics.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski <[hidden email]> wrote:  This works great: Really ? surprising given it is the EXACT same for-loop as in your original problem with counter "i" replaced by "k" and reorder to matrix[!100]<- 0 instead of matrix(0)[i]<- 100 You didn't even attempt to implement Carl's suggestion to use apply family for looping (which I still think is completely unnecessary). The only logical conclusion is N=nrow(input) was not large enough to pose a problem in the first place. In the future please use some brain power before waisting ours. Cheers > > input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) > result<-input > N<-nrow(input) > for (k in 1:N){ >  foo <- which (input == k,arr.ind=T) >  result[k,foo[2]] <-100 > } > result[result !=100]<-0 > > Dimitri > > > On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft <[hidden email]> wrote: >> I think the OP wants to fill values in an arbitrarily large matrix. Now, >> first of all, I'd like to know what his real problem is, since this seems >> like a very tedious and unproductive matrix to produce.  But in the >> meantime,  since he also left out important information, let's assume the >> input matrix is N rows by M columns, and that he wants therefore to end up >> with N instances of "100", not counting the original value of 100 that is >> one of his ranking values (a bad BAD move IMHO). >> >> Then either loop or lapply over an equation like (I've expanded things more >> than necessary for clarity >> result<-inmatrix >> for (k in 1:N){ >> foo <- which (inmatrix == k,arr.ind=T) >> result[k,foo[2]] <-100 >> >> } >> >> >> >> I maybe missing something but this seems like an indexing problem >> which doesn't require a loop at all. Something like this maybe? >> >> (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2)) >> output <- matrix(0,max(input),2) >> output[input[,1],1] <- 100 >> output[input[,2],2] <- 100 >> output >> >> Cheers >> >> >> On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski >> wrote: >>> Hello, everybody! >>> >>> I have a matrix "input" (see example below) - with all unique entries >>> that are actually unique ranks (i.e., start with 1, no ties). >>> I want to assign a value of 100 to the first row of the column that >>> contains the minimum (i.e., value of 1). >>> Then, I want to assign a value of 100 to the second row of the column >>> that contains the value of 2, etc. >>> The results I am looking for are in "desired.results". >>> My code (below) does what I need. But it's using a loop through all >>> the rows of my matrix and searches for a matrix element every time. >>> My actual matrix is very large. Is there a way to do it more efficiently? >>> Thank you very much for the tips! >>> Dimitri >>> >>> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) >>> (input) >>> desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100))) >>> (desired.result) >>> result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0))) >>> for(i in 1:nrow(input)){ # i<-1 >>>  mymin<-i >>>  mycoords<-which(input==mymin,arr.ind=TRUE) >>>  result[i,mycoords[2]]<-100 >>>  input[mycoords]<-max(input) >>> } >>> (result) >>> >> -- >> >> Sent from my Cray XK6 >> "Quidvis recte factum, quamvis humile, praeclarum." >> >> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html>> and provide commented, minimal, self-contained, reproducible code. > > > > -- > Dimitri Liakhovitski > marketfusionanalytics.com > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by Rui Barradas Hello, Oops! What happened to the function 'f'? Forgot to copy and pasted only the rest, now complete. f <- function(x){         nr <- nrow(x)         result <- matrix(0, nrow=nr, ncol=ncol(x))         colnames(result) <- colnames(x)         inp.ord <- order(x)[1:nr] - 1 # Keep only one per row, must be zero based         inx <- cbind(1:nr, inp.ord %/% nr + 1) # Index matrix         result[inx] <- 100         result } # Original example input <- as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8))) (input) desired.result <- as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100))) (desired.result) all.equal(f(input), desired.result) # Two other examples set.seed(123) (x <- matrix(sample(10, 10), ncol=2)) f(x) (y <- matrix(sample(40, 40), ncol=5)) f(y) Note that there's no loops (or apply, which is also a loop.) Rui Barradas
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by ilai-2 Sorry, I didn't have time to check the speed, indeed. However - isn't apply the same as a loop, just hidden? D. On Fri, Apr 6, 2012 at 6:59 PM, ilai <[hidden email]> wrote: > On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski > <[hidden email]> wrote: >  This works great: > > Really ? surprising given it is the EXACT same for-loop as in your > original problem with counter "i" replaced by "k" and reorder to > matrix[!100]<- 0 instead of matrix(0)[i]<- 100 > You didn't even attempt to implement Carl's suggestion to use apply > family for looping (which I still think is completely unnecessary). > > The only logical conclusion is N=nrow(input) was not large enough to > pose a problem in the first place. In the future please use some brain > power before waisting ours. > > Cheers > >> >> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) >> result<-input >> N<-nrow(input) >> for (k in 1:N){ >>  foo <- which (input == k,arr.ind=T) >>  result[k,foo[2]] <-100 >> } >> result[result !=100]<-0 >> >> Dimitri >> >> >> On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft <[hidden email]> wrote: >>> I think the OP wants to fill values in an arbitrarily large matrix. Now, >>> first of all, I'd like to know what his real problem is, since this seems >>> like a very tedious and unproductive matrix to produce.  But in the >>> meantime,  since he also left out important information, let's assume the >>> input matrix is N rows by M columns, and that he wants therefore to end up >>> with N instances of "100", not counting the original value of 100 that is >>> one of his ranking values (a bad BAD move IMHO). >>> >>> Then either loop or lapply over an equation like (I've expanded things more >>> than necessary for clarity >>> result<-inmatrix >>> for (k in 1:N){ >>> foo <- which (inmatrix == k,arr.ind=T) >>> result[k,foo[2]] <-100 >>> >>> } >>> >>> >>> >>> I maybe missing something but this seems like an indexing problem >>> which doesn't require a loop at all. Something like this maybe? >>> >>> (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2)) >>> output <- matrix(0,max(input),2) >>> output[input[,1],1] <- 100 >>> output[input[,2],2] <- 100 >>> output >>> >>> Cheers >>> >>> >>> On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski >>> wrote: >>>> Hello, everybody! >>>> >>>> I have a matrix "input" (see example below) - with all unique entries >>>> that are actually unique ranks (i.e., start with 1, no ties). >>>> I want to assign a value of 100 to the first row of the column that >>>> contains the minimum (i.e., value of 1). >>>> Then, I want to assign a value of 100 to the second row of the column >>>> that contains the value of 2, etc. >>>> The results I am looking for are in "desired.results". >>>> My code (below) does what I need. But it's using a loop through all >>>> the rows of my matrix and searches for a matrix element every time. >>>> My actual matrix is very large. Is there a way to do it more efficiently? >>>> Thank you very much for the tips! >>>> Dimitri >>>> >>>> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) >>>> (input) >>>> desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100))) >>>> (desired.result) >>>> result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0))) >>>> for(i in 1:nrow(input)){ # i<-1 >>>>  mymin<-i >>>>  mycoords<-which(input==mymin,arr.ind=TRUE) >>>>  result[i,mycoords[2]]<-100 >>>>  input[mycoords]<-max(input) >>>> } >>>> (result) >>>> >>> -- >>> >>> Sent from my Cray XK6 >>> "Quidvis recte factum, quamvis humile, praeclarum." >>> >>> >>> ______________________________________________ >>> [hidden email] mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html>>> and provide commented, minimal, self-contained, reproducible code. >> >> >> >> -- >> Dimitri Liakhovitski >> marketfusionanalytics.com >> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html>> and provide commented, minimal, self-contained, reproducible code. -- Dimitri Liakhovitski marketfusionanalytics.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by Rui Barradas Thank you very much, Rui. This definitely produces the result needed. Again, I have not checked the speed yet. input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) f <- function(x){        nr <- nrow(x)        result <- matrix(0, nrow=nr, ncol=ncol(x))        colnames(result) <- colnames(x)        inp.ord <- order(x)[1:nr] - 1 # Keep only one per row, must be zero based        inx <- cbind(1:nr, inp.ord %/% nr + 1) # Index matrix        result[inx] <- 100        result } f(input) Dimitri On Fri, Apr 6, 2012 at 7:02 PM, Rui Barradas <[hidden email]> wrote: > Hello, > > Oops! > > What happened to the function 'f'? > Forgot to copy and pasted only the rest, now complete. > > > f <- function(x){ >        nr <- nrow(x) >        result <- matrix(0, nrow=nr, ncol=ncol(x)) >        colnames(result) <- colnames(x) > >        inp.ord <- order(x)[1:nr] - 1 # Keep only one per row, must be zero based >        inx <- cbind(1:nr, inp.ord %/% nr + 1) # Index matrix >        result[inx] <- 100 >        result > } > > # Original example > input <- as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8))) > (input) > desired.result <- as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100))) > (desired.result) > all.equal(f(input), desired.result) > > # Two other examples > set.seed(123) > (x <- matrix(sample(10, 10), ncol=2)) > f(x) > > (y <- matrix(sample(40, 40), ncol=5)) > f(y) > > Note that there's no loops (or apply, which is also a loop.) > > Rui Barradas > > > -- > View this message in context: http://r.789695.n4.nabble.com/filling-the-matrix-row-by-row-in-the-order-from-lower-to-larger-elements-tp4538171p4538486.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. -- Dimitri Liakhovitski marketfusionanalytics.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 Preoccupation with speed of execution is typically (certainly not always) misplaced. Provided you have used sensible basic vectorization, loops in whatever form work adequately. First get working code. Then, if necessary, parallelization, byte compilation, or complex vectorization strategies can be employed. And, of course, there's always C code for those who know it. -- Bert On Sun, Apr 8, 2012 at 7:31 AM, Dimitri Liakhovitski <[hidden email]> wrote: > Thank you very much, Rui. > This definitely produces the result needed. > Again, I have not checked the speed yet. > > input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) > f <- function(x){ >       nr <- nrow(x) >       result <- matrix(0, nrow=nr, ncol=ncol(x)) >       colnames(result) <- colnames(x) > >       inp.ord <- order(x)[1:nr] - 1 # Keep only one per row, must be zero based >       inx <- cbind(1:nr, inp.ord %/% nr + 1) # Index matrix >       result[inx] <- 100 >       result > } > f(input) > > Dimitri > > On Fri, Apr 6, 2012 at 7:02 PM, Rui Barradas <[hidden email]> wrote: >> Hello, >> >> Oops! >> >> What happened to the function 'f'? >> Forgot to copy and pasted only the rest, now complete. >> >> >> f <- function(x){ >>        nr <- nrow(x) >>        result <- matrix(0, nrow=nr, ncol=ncol(x)) >>        colnames(result) <- colnames(x) >> >>        inp.ord <- order(x)[1:nr] - 1 # Keep only one per row, must be zero based >>        inx <- cbind(1:nr, inp.ord %/% nr + 1) # Index matrix >>        result[inx] <- 100 >>        result >> } >> >> # Original example >> input <- as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8))) >> (input) >> desired.result <- as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100))) >> (desired.result) >> all.equal(f(input), desired.result) >> >> # Two other examples >> set.seed(123) >> (x <- matrix(sample(10, 10), ncol=2)) >> f(x) >> >> (y <- matrix(sample(40, 40), ncol=5)) >> f(y) >> >> Note that there's no loops (or apply, which is also a loop.) >> >> Rui Barradas >> >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/filling-the-matrix-row-by-row-in-the-order-from-lower-to-larger-elements-tp4538171p4538486.html>> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html>> and provide commented, minimal, self-contained, reproducible code. > > > > -- > Dimitri Liakhovitski > marketfusionanalytics.com > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: filling the matrix row by row in the order from lower to larger elements

 In reply to this post by Dimitri Liakhovitski-2 On Sun, Apr 8, 2012 at 8:26 AM, Dimitri Liakhovitski <[hidden email]> wrote: > Sorry, I didn't have time to check the speed, indeed. > However - isn't apply the same as a loop, just hidden? > D. > Yes ?apply is a loop but not the same as ?for, see "Intro to R". As Bert Gunter pointed out the issue here was not speed but that your problem could have been vectorized to avoid loops all together (which would have the added benefit of speed). This was given to you in my first response which assumed a small number of columns in the matrix, but Rui gave an elegant expansion to use on any ncol(matrix). Using apply was suggested at some point by others, my comment was simply that you failed to meet even that adjustment. Cheers > On Fri, Apr 6, 2012 at 6:59 PM, ilai <[hidden email]> wrote: >> On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski >> <[hidden email]> wrote: >>  This works great: >> >> Really ? surprising given it is the EXACT same for-loop as in your >> original problem with counter "i" replaced by "k" and reorder to >> matrix[!100]<- 0 instead of matrix(0)[i]<- 100 >> You didn't even attempt to implement Carl's suggestion to use apply >> family for looping (which I still think is completely unnecessary). >> >> The only logical conclusion is N=nrow(input) was not large enough to >> pose a problem in the first place. In the future please use some brain >> power before waisting ours. >> >> Cheers >> >>> >>> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) >>> result<-input >>> N<-nrow(input) >>> for (k in 1:N){ >>>  foo <- which (input == k,arr.ind=T) >>>  result[k,foo[2]] <-100 >>> } >>> result[result !=100]<-0 >>> >>> Dimitri >>> >>> >>> On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft <[hidden email]> wrote: >>>> I think the OP wants to fill values in an arbitrarily large matrix. Now, >>>> first of all, I'd like to know what his real problem is, since this seems >>>> like a very tedious and unproductive matrix to produce.  But in the >>>> meantime,  since he also left out important information, let's assume the >>>> input matrix is N rows by M columns, and that he wants therefore to end up >>>> with N instances of "100", not counting the original value of 100 that is >>>> one of his ranking values (a bad BAD move IMHO). >>>> >>>> Then either loop or lapply over an equation like (I've expanded things more >>>> than necessary for clarity >>>> result<-inmatrix >>>> for (k in 1:N){ >>>> foo <- which (inmatrix == k,arr.ind=T) >>>> result[k,foo[2]] <-100 >>>> >>>> } >>>> >>>> >>>> >>>> I maybe missing something but this seems like an indexing problem >>>> which doesn't require a loop at all. Something like this maybe? >>>> >>>> (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2)) >>>> output <- matrix(0,max(input),2) >>>> output[input[,1],1] <- 100 >>>> output[input[,2],2] <- 100 >>>> output >>>> >>>> Cheers >>>> >>>> >>>> On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski >>>> wrote: >>>>> Hello, everybody! >>>>> >>>>> I have a matrix "input" (see example below) - with all unique entries >>>>> that are actually unique ranks (i.e., start with 1, no ties). >>>>> I want to assign a value of 100 to the first row of the column that >>>>> contains the minimum (i.e., value of 1). >>>>> Then, I want to assign a value of 100 to the second row of the column >>>>> that contains the value of 2, etc. >>>>> The results I am looking for are in "desired.results". >>>>> My code (below) does what I need. But it's using a loop through all >>>>> the rows of my matrix and searches for a matrix element every time. >>>>> My actual matrix is very large. Is there a way to do it more efficiently? >>>>> Thank you very much for the tips! >>>>> Dimitri >>>>> >>>>> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8))) >>>>> (input) >>>>> desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100))) >>>>> (desired.result) >>>>> result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0))) >>>>> for(i in 1:nrow(input)){ # i<-1 >>>>>  mymin<-i >>>>>  mycoords<-which(input==mymin,arr.ind=TRUE) >>>>>  result[i,mycoords[2]]<-100 >>>>>  input[mycoords]<-max(input) >>>>> } >>>>> (result) >>>>> >>>> -- >>>> >>>> Sent from my Cray XK6 >>>> "Quidvis recte factum, quamvis humile, praeclarum." >>>> >>>> >>>> ______________________________________________ >>>> [hidden email] mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-help>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html>>>> and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >>> -- >>> Dimitri Liakhovitski >>> marketfusionanalytics.com >>> >>> ______________________________________________ >>> [hidden email] mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html>>> and provide commented, minimal, self-contained, reproducible code. > > > > -- > Dimitri Liakhovitski > marketfusionanalytics.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.