How to find the position of a number in an array?

65 ビュー (過去 30 日間)
CS
CS 2020 年 6 月 12 日
編集済み: Matt J 2020 年 6 月 13 日
I have a column vector Y which is 7306409x1 double. I want to find the position (indices) of a specifric number (8) in Y. I used
k=find(Y==8)
But MATLAB gives the answer
k =
0×1 empty double column vector
and does not show the indices. I think the problem is something with the "double". Does anyone know how to solve this?
Thanks!
  1 件のコメント
madhan ravi
madhan ravi 2020 年 6 月 12 日
Is Y integer or double?

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

回答 (2 件)

Matt J
Matt J 2020 年 6 月 12 日
編集済み: Matt J 2020 年 6 月 12 日
There is nothing wrong with your code as long as there are elements in Y that are exactly equal to 8
>> Y=[8 1 2 5 8 6];
>> k=find(Y==8)
k =
1 5
Suprises may happen, however, if Y contains floating point inaccuracies that you can't see on the screen,
>> Y=[8 1 2 5 8 6] + 10*eps,
Y =
8.0000 1.0000 2.0000 5.0000 8.0000 6.0000
>> k=find(Y==8)
k =
1×0 empty double row vector
One solution is to round,
>> k=find(round(Y)==8)
k =
1 5
  4 件のコメント
CS
CS 2020 年 6 月 12 日
format long will show the long format of numbers in the command window. My question is regarding to how show the long format of numbers in the Variables window?
Matt J
Matt J 2020 年 6 月 13 日
編集済み: Matt J 2020 年 6 月 13 日
My question is regarding to how show the long format of numbers in the Variables window?
That can be adjusted in the view tab
However, rather than relying on copy-paste, I think it would be better to simply do,
k=find(Y==Y(283))

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


madhan ravi
madhan ravi 2020 年 6 月 12 日
編集済み: madhan ravi 2020 年 6 月 12 日
k = find( (abs(Y) - 8) < 1e-2 ) % where 1e-2 is the tolerance

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by