[R] index values of one matrix to another of a different size

Rui Barradas rui1174 at sapo.pt
Fri Mar 9 21:23:31 CET 2012


Hello,

I don't know if it's the fastest but it's more natural to have an index
matrix with two columns only,
one for each coordinate. And it's fast.

fun <- function(valdata, inxdata){
	nr <- nrow(inxdata)
	nc <- ncol(inxdata)
	mat <- matrix(NA, nrow=nr*nc, ncol=2)
	i1 <- 1
	i2 <- nr
	for(j in 1:nc){
		mat[i1:i2, 1] <- inxdata[, j]
		mat[i1:i2, 2] <- rep(j, nr)
		i1 <- i1 + nr
		i2 <- i2 + nr
	}
	matrix(valdata[mat], ncol=nc)
}

fun(vals, indx)

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Re-index-values-of-one-matrix-to-another-of-a-different-size-tp4458666p4460575.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list