使用向量的MATLAB sub2ind [英] MATLAB sub2ind using vectors

查看:89
本文介绍了使用向量的MATLAB sub2ind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个矩阵A

A = magic(5)

17    24     1     8    15
23     5     7    14    16
 4     6    13    20    22
10    12    19    21     3
11    18    25     2     9

现在,我使用

A(1:2, 1:2)

17    24
23     5

现在我需要由(1:2,1; 2)给出的线性索引为(1 2 6 7).使用sub2ind:

Now I need the linear index given by (1:2, 1;2) which are (1 2 6 7). Using sub2ind:

sub2ind(size(A),[1:2], [1:2])

但是此命令仅返回(1 7)我该如何解决呢?

But this command returns just (1 7) how can I solve this?

推荐答案

假设您要选择A(1:2,2:3):

% Row and column indexes
rind = 1:2;
cind = 2:3;

pos  = bsxfun(@plus,rind', size(A,2)*(cind-1));

pos =
     6    11
     7    12

您可能希望将其重塑为列向量pos(:),或者在一行中调用reshape().

You might want to reshape it into a column vector pos(:), or in one line with a call to reshape().

这篇关于使用向量的MATLAB sub2ind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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