I need to build a matrix from data that is stored in several other matrices that all have a pointer in their first column. This is how the original matrices might look, with a-e being the pointers connecting the the data from all the matrices and the v-z being the data that is linked together. The arrow points to what I want my final matrix to look like.
 a  x   x
 b  y   y
 c  z   z
 d  w   w           
 e  v   v           
 e  v   v          
 d  w   w          
 c  z   z
 b  y   y
 a  x   x
----->
x x x x
y y y y 
z z z z
w w w w
v v v v
I cant seem to write the right algorithm to do this, I am either getting subscript out of bounds errors or replacement has length zero errors. Here is what I have now but it is not working.
for(i in 1:length(matlist)){
tempmatrix = matlist[[i]]                  # list of matrices to be combined 
genMatrix[1,i] = tempmatrix[1,2]
for(j in 2:length(tempmatrix[,1])){
  index = which(indexv == tempmatrix[j,1]) #the row index for the data that needs to be match 
                                            # with an ECID
  for(k in 1:length(tempmatrix[1,])){
    genMatrix[index,k+i] = tempmatrix[j,k]
  }
                # places the data in same row as the ecid
}
}
 print(genMatrix)
EDIT: I just want to clarify that my example only shows two matrices but in the list matlist there can be any number of matrices. I need to find a way of merging them without having to know how many matrices are in matlist at the time.
 
    