在MATLAB中模拟级联索引的一些好方法是什么?
问题描述:
例如我想做这样的事情:
E.g. I would like to do things such as:
A=4:20;
find(A>5)(2) % want to access the 2nd element of the index array returned by find
答
是的,此相当 经常在不同的上下文中 >,并且单行答案为subsref
.对于您的情况,就是这样:
Yes, this comes up fairly frequently in different contexts, and the one-line answer is subsref
. For your case, it is this:
subsref(find(A>5),struct('type','()','subs',{{2}}))
更清洁的解决方案使用未记录的builtin
:
A much cleaner solution uses an undocumented builtin
:
builtin('_paren',find(A>5),2)
作为丑陋语法或未记录功能的替代方法,您可以定义一个类似于以下内容的小型函数,
As an alternative to ugly syntax or undocumented functionality, you could define a small function like the following,
function outarray = nextind(inarray,inds)
outarray = inarray(inds);
或内联函数:
nextind = @(v,ii) v(ii);
并像nextind(find(A>5),2)
一样调用它.这比subsref
干净,如果要执行线性索引(不是下标),则很好.
And call it like nextind(find(A>5),2)
. This is cleaner than subsref
and good if you are doing linear indexing (not subscripts).