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
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
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}}))
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);
Or an inline function:
nextind = @(v,ii) v(ii);
And call it like nextind(find(A>5),2)
. This is cleaner than subsref
and good if you are doing linear indexing (not subscripts).