Converting between matrix subscripts and linear indices (like ind2sub/sub2ind in matlab)

前端 未结 6 1904
遥遥无期
遥遥无期 2021-01-01 20:37

Let\'s say you have a matrix

m <- matrix(1:25*2, nrow = 5, ncol=5)

How do you go from matrix subscripts (row index, column index) to a lin

6条回答
  •  攒了一身酷
    2021-01-01 20:41

    You mostly don't need those functions in R. In Matlab you need those because you can't do e.g.

    A(i, j) = x

    where i,j,x are three vectors of row and column indices and x contains the corresponding values. (see also this question)

    In R you can simply:

    A[ cbind(i, j) ] <- x

提交回复
热议问题