Convert Index values from "find" function into coordinates.

Hello! I am using the find function in my code. If I want to find where all the 2's are in a matrix, suppose, this function will spit out the indices of all the 2 values, but it does so in an interesting way:
a =
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
Now, if I implement the function, find(a==2), I get:
ans =
8
9
11
Notice how it counts down each row until it reaches a 2. I would like a coordinate instead; I would like a (row,column) coordinate. Any ideas on how I can do this using the find function?
Much appreciated! Sean

回答 (1 件)

Jan
Jan 2017 年 1 月 28 日
編集済み: Jan 2017 年 1 月 28 日

1 投票

When you have a question concerning a specific command, reading the corresponding docs helps. See doc find :
[row, col] = find(a == 2)
:-)

5 件のコメント

Sean Farrell
Sean Farrell 2017 年 1 月 28 日
Ahh yes! I actually used this [row,col] feature of the find function the other day. I just did not connect it to this problem. Thank you very much!
Jan
Jan 2017 年 1 月 28 日
You are welcome.
Daniel Garside
Daniel Garside 2018 年 2 月 23 日
Is there a neat way to do this for n-dimensional arrays?
(I feel like I've seen a trick somewhere but can't find it again...)
Stephen23
Stephen23 2018 年 2 月 23 日
編集済み: Stephen23 2018 年 2 月 23 日
@Daniel Garside: use ind2sub:
[R,C,P,...] = ind2sub(size(A),find(A==2))
Jos (10584)
Jos (10584) 2018 年 2 月 23 日
And without specifying the number of dimensions:
[subI{1:ndims(A)}] = ind2sub(size(A), find(A==2))

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

カテゴリ

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

質問済み:

2017 年 1 月 28 日

コメント済み:

2018 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by