Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 RI 中有一个矩阵,比如说:
1 0 1 2 0 0 3 2 0 2 0 2 2 1 0 1 2 1 3 2 3 4 0 1 2 1 1 0
我想将此矩阵转换为累积版本,以便得到矩阵:
1 1 2 4 4 4 7 2 2 4 4 6 8 9 0 1 3 4 7 9 12 4 4 5 7 8 9 9
所以矩阵 2 中的[i,j]第 th 项是矩阵 1 中所有元素的总和,使得i_1 <= i_2. 我怎么能在 R 中做到这一点?
[i,j]
i_1 <= i_2
你可以做:
> t(apply(mat, 1, cumsum))
或者
> ave(mat, row(mat), FUN = cumsum) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 1 2 4 4 4 7 [2,] 2 2 4 4 6 8 9 [3,] 0 1 3 4 7 9 12 [4,] 4 4 5 7 8 9 9
mat你的原始矩阵在哪里。
mat