Finding the index of spesific element in a matrix?

674 ビュー (過去 30 日間)
IBM watson
IBM watson 2019 年 5 月 23 日
コメント済み: Ulika Naidoo 2021 年 1 月 22 日
Lets say we have matrix:
a=[ 2, 3, 5
3, 4, 7
4, 4, 4] ;
I want to know index of a(4) which is the bold 3. This is just an example. I want to learn how to find index of a(n).

採用された回答

madhan ravi
madhan ravi 2019 年 5 月 23 日
編集済み: madhan ravi 2019 年 5 月 23 日
Assuming you want to index of the last match in the matrix:
Index=find(a==3,1,'last')
  13 件のコメント
madhan ravi
madhan ravi 2019 年 5 月 23 日
編集済み: madhan ravi 2019 年 5 月 23 日
No problem, by the way the original question you asked is totally irrelevant to your latter comment. Please don’t do this in the future riddling the answers.
Look into ind2sub() function to convert the linear indices. To understand what this statement means , you need to read the link which I shared thoroughly and practice. And finally have a look at ind2sub().
IBM watson
IBM watson 2019 年 5 月 23 日
Sure, I didn't do it on purpose. Just could't explain clearly what I want. Thanks again.

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

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 23 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 23 日
I want to know index of a(4) which is the bold 3
here ......................... this 4^ represent the index, I guessing you are looking for following one.
idx=find(a==3);
One dimentional case array case
>> a=[2,3,5,3,4,7,4,4,4];
>> idx=find(a==7)
idx =
6
>> idx=find(a==3)
idx =
2 4
But Matrices have 2 dimention so indices represents (x,y), where answer will be little different
>> a=magic(3)
a =
8 1 6
3 5 7
4 9 2
>> [r c]=find(a==3)
r =
2
c =
1
  5 件のコメント
Stephen23
Stephen23 2020 年 3 月 5 日
"why c =1 ? "
Because the value 3 is in the 1st column.
Ulika Naidoo
Ulika Naidoo 2021 年 1 月 22 日
Thank you!

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by