且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R等同于Matlab中的ind2sub/sub2ind

更新时间:2022-04-20 21:09:52

这不是我以前使用过的东西,但是根据

This is not something I've used before, but according to this handy dandy Matlab to R cheat sheet, you might try something like this, where m is the number of rows in the matrix, r and c are row and column numbers respectively, and ind the linear index:

MATLAB:

[r,c] = ind2sub(size(A), ind)

R:

r = ((ind-1) %% m) + 1
c = floor((ind-1) / m) + 1

MATLAB:

ind = sub2ind(size(A), r, c)

R:

ind = (c-1)*m + r