s-news
[Top] [All Lists]

[S] Matrix Operation (summary)

To: S-news@wubios.wustl.edu
Subject: [S] Matrix Operation (summary)
From: Fang Chen <fangc@stat.cmu.edu>
Date: Thu, 27 Jan 2000 23:48:04 -0500 (EST)
In-reply-to: <Pine.HPP.4.10.10001262059140.29934-100000@excess.stat.cmu.edu>
Sender: owner-s-news@wubios.wustl.edu

Many thanks to Nick Ellis, Rachel Fewster, James Stapleton,
Julian Taylor, Bill Venables, S.D.Byers, Jim Stapleton, 
Don MacQueen, Chuck Taylor, and Phil Spector. Thanks to so
many quick responses - this really is a great community.

A few people used the function row(), it works very well. Julian
Taylor used split(), and it took me a while to understand how it works.

Fang Chen

My original post is:

> Hello all:
> 
> I need help on how to better perform a matrix operation. 
> 
> Data_matrix(c(2,3,5,6,3,4,5,7,8,10,11,13,4,5,6),3,5)
> Row.Index_matrix(c(1,2,4,2,3,5,3,4,5),3,3,byrow=T)
> > Data
>      [,1] [,2] [,3] [,4] [,5] 
> [1,]    2    6    5   10    4
> [2,]    3    3    7   11    5
> [3,]    5    4    8   13    6
> > Row.Index
>      [,1] [,2] [,3] 
> [1,]    1    2    4
> [2,]    2    3    5
> [3,]    3    4    5
> 
> And I hope to pick out entries in Data, row by row, according to
> Row.Index,
> i.e. I want my final matrix looks like:
>      [,1] [,2] [,3] 
> [1,]    2    6   10
> [2,]    3    7    5
> [3,]    8   13    6
> 

### Solutions from Nick Ellis:
x<-matrix(1:15,3,5)
y<-matrix(c(1,2,3,2,3,4,3,4,5),3,3,T)
t(apply(row(x)[,1,drop=F],1,function(i,x,y)x[i,y[i,]],x,y))

ix <- as.vector(row(y))
iy <- as.vector(y)
x[cbind(ix,iy)]
matrix(x[cbind(ix,iy)],nrow(x))


### Solution from Rachel Fewster 

n1 <- nrow(Data)
n2 <- ncol(Data)
n3 <- ncol(Row.Index)
matrix(t(Data)[rep(seq(0, (n1-1)*n2, n2), rep(n3, n1)) + 
as.vector(t(Row.Index))], n1, n3, byrow=T)


### From James Stapleton

rowindexfcn_function(Data, row.index)
{
        d <- dim(row.index)
        R <- Data[cbind(1:d[1], row.index[, 1])]
        for(i in 2:d[2])
                R <- rbind(R, Data[cbind(1:d[1], row.index[, i])])
        R
}



### From: Julian Taylor 

splitData <- split(as.data.frame(t(Data[, c(Row.Index)])), rep(1:3, rep(3,3)))
sapply(splitData, diag)

### From Bill Venables

Data <- matrix(c(2,3,5,6,3,4,5,7,8,10,11,13,4,5,6),3,5)
Row.Index <- matrix(c(1,2,4,2,3,5,3,4,5),3,3,byrow=T)
index <- cbind(as.vector(row(Row.Index)), as.vector(Row.Index))
newData <- matrix(Data[index], nrow=3)

### From  S.D.Byers 

matrix(Data[cbind(c(t(row(Row.Index))),c(t(Row.Index)))],
byrow=T,ncol=dim(Row.Index)[1])

## Also from S.D.Byers

matrix(Data[cbind(c(row(Row.Index)),c(Row.Index))],
byrow=F,ncol=dim(Row.Index)[1])

### From: Jim Stapleton 

rowindexfcn2_function(Data, row.index)
{
        n <- dim(Data)[1]
        k <- dim(row.index)[2]
        A <- matrix(0, n, k)
        for(i in 1:n)
                A[i,  ] <- Data[i, row.index[i,  ]]
        A
}


### From Don MacQueen

tmp <- cbind(Data,Row.Index)
t(apply(tmp,1,function(x) x[x[6:8]]))


### From Chuck Taylor

final <- matrix(nrow=nrow(Data), ncol=ncol(Row.Index))
for (i in 1:nrow(Row.Index)) 
    final[i,] <- Data[i,Row.Index[i,]]


###From Phil Spector

nr <- nrow(Row.Index)
result <- t(sapply(1:nr,function(x)Data[cbind(rep(x,nr),Row.Index[x,])]))


yours

Fang Chen

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>