reverse indexing with conditions

Let A = [1 2 4 6 10], I want to find the indices of the matrix for which the element less than 5.
So if I say: A < 5, then it will return [1 1 1 0 0]. How can I proceed to get the index of all those 1's?
Thanks

 採用された回答

TastyPastry
TastyPastry 2015 年 11 月 9 日

0 投票

idx = 1:numel(A);
mask = A < 5;
idx = idx(mask);

その他の回答 (1 件)

Thorsten
Thorsten 2015 年 11 月 9 日
編集済み: Thorsten 2015 年 11 月 9 日

1 投票

To get numerical indices, use find
idx = find(A < 5);
You can also use logical indices, that are often faster:
idx = A < 5;
In both cases you get the indexed numbers using
A(idx)

1 件のコメント

cgo
cgo 2015 年 11 月 9 日
perfect. thanks

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

cgo
2015 年 11 月 9 日

コメント済み:

cgo
2015 年 11 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by