R等同于Matlab中的ind2sub/sub2ind [英] R's equivalent to ind2sub/sub2ind in matlab

查看:72
本文介绍了R等同于Matlab中的ind2sub/sub2ind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matlab具有两个有用的函数,用于将矩阵下标转换为线性索引,反之亦然. (ind2sub和sub2ind)

Matlab has two useful functions for converting matrix subscripts to linear indices and vice versa. (ind2sub and sub2ind)

R中有等效的方法吗?

Is there an equivalent way in R?

推荐答案

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

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

这篇关于R等同于Matlab中的ind2sub/sub2ind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆