Find an element of a matrix equal to a constant

Hi,
If B is a matrix i need to find the index of element that is equal to a constant c where the constant c is undoubtedly somewhere in the matrix.
I tried to do:
B(:,B(2,:)==c)
but there are some problems with logical operator ==
It returns me "Empty matrix"

回答 (1 件)

Matt Fig
Matt Fig 2011 年 3 月 29 日

0 投票

If you are looking for the linear index:
I = find(B==c);
If you are looking for the subscript index:
[I,J] = find(B==c);
I hope you are not experiencing a floating point issue???
Whenever I hear somebody say they cannot find a value that they just know is in an array, I wonder if they are not about to learn the joys of floating point arithmetic.

3 件のコメント

cyberdyne
cyberdyne 2011 年 3 月 29 日
I'm looking for subscript index but after i enter your code it retuns again:
Empty Matrix
Walter Roberson
Walter Roberson 2011 年 3 月 29 日
Yup, see the link Matt posted about floating point issues.
cyberdyne
cyberdyne 2011 年 3 月 29 日
Great! The problem was it. Thanks.

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

カテゴリ

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

質問済み:

2011 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by