3

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
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • 1
    I just thought of an alternative solution, and updated my answer. Anyway, it's certainly not the first time something similar has been asked, but... http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – chappjc Oct 10 '13 at 22:35

1 Answers1

3

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).

Community
  • 1
  • 1
chappjc
  • 30,359
  • 6
  • 75
  • 132