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

Rui Barradas rui1174 at sapo.pt
Fri Apr 6 23:09:07 CEST 2012


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


--
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-tp4538171p4538334.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list