Is there a clever way to ditch 'find' and use logical indexing here ?

1 回表示 (過去 30 日間)
Ivan
Ivan 2012 年 7 月 9 日
I'm sorting an array x:
[~, ind] = sort(x)
If I want the indices of the (n) smallest elements of x, I'll simply go for:
y = ind(1:n)
Now if I want the indices of the n smallest that also are > 0.5, I go:
valid = x > 0.5
y = ind(find(valid(ind), n, 'first'))
Code Analyzer is obviously whining about using 'find', but I wonder if there really is a way to do this using only logical indexing ?
I need the indices -or logical indices- not the values themselves. I use the indices in another array that needs to have the same size as x. So I can't narrow down x to z = x > 0.5 and then sort z: z-based indices wouldn't be consistent with the size of x in that case.
Thanks
Ivan

採用された回答

Jonathan Sullivan
Jonathan Sullivan 2012 年 7 月 9 日
sorted_x = sort(x)
logical_inds = x > 0.5 & x <= sorted_x(n);
  1 件のコメント
Ivan
Ivan 2012 年 7 月 9 日
But doing this I will possibly end up with less than n values. I do need n values, though.
I need the (x-based indices of the) n smallest in x that are > 0.5. Not the subset of the n smallest that are also > 0.5.
Not sure it's doable with logical only...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by