find coordinates within a matrix

12 ビュー (過去 30 日間)
AA
AA 2020 年 5 月 3 日
コメント済み: Ameer Hamza 2020 年 5 月 3 日
Suppose I have the following matrices:
b= 0.85981
0.87169
and
a=
0.855750000000000 0.858420000000000 0.858110000000000 0.858210000000000 0.858880000000000
0.857680000000000 0.858700000000000 0.858920000000000 0.858480000000000 0.858630000000000
The first value of "b" (0.8591) is contained in the first row of variable "a" whilst the second value of "b" (0.87169) is found in the second row of variable "a".
My question is how can I find which column number or position "b" has in "a"?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 3 日
編集済み: Ameer Hamza 2020 年 5 月 3 日
the elements of 'b' given in the question are not actually present in the matrix 'a', so I used values in b from 'a'
a = [
0.855750000000000 0.858420000000000 0.858110000000000 0.858210000000000 0.858880000000000
0.857680000000000 0.858700000000000 0.858920000000000 0.858480000000000 0.858630000000000];
b = [0.85811
0.8587];
[~,c] = find(a==b);
Result:
c =
2
3
Note that, if the values are not exactly the same, then you will need to do the comparison with a tolerance value like this
[~,c] = find(abs(a-b)<1e-6); % 1e-6 is tolerance value
  2 件のコメント
Image Analyst
Image Analyst 2020 年 5 月 3 日
Or simply use ismembertol() in recent versions of MATLAB.
Ameer Hamza
Ameer Hamza 2020 年 5 月 3 日
Tolerance value in ismembertol is always a bit non-intuitive. I always find it easier to write it using abs().

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by