Approximately equal or egual to +- error

15 ビュー (過去 30 日間)
Stefan
Stefan 2011 年 6 月 8 日
コメント済み: Manjur Hossain 2021 年 5 月 7 日
Hi,
I have two vectors where each has a column with approximately equal values like this:
Vector1 Vector2
1.11111 1.2222
1.12111 1.2238
... ...
1.2223 1.4446
Since the value of Vector1: 1.2223 is not exactly equal to the value of Vector2: 1.2222 I cannot use the equal to command to find the value 1.2223 in vector2.
What I'm looking for is a command or a way to find a value in vector2 that is approximately equal to a value from vector1 within some limits.
It could look like this:
find(vector2(:,1))=vector1(1,?)+-0.0001
Where +-0.0001 would be the limits. But you can't write it like this.
I'm thankful for any hint.

回答 (4 件)

Andrei Bobrov
Andrei Bobrov 2011 年 6 月 8 日
u(abs(v - u)<=e)
index
idx = find(abs(v - u)<=e)

Paulo Silva
Paulo Silva 2011 年 6 月 8 日
Adapt this code to your purpose:
v=[1.1 2.2 3.3]' %vector 1
u=[1.2 2.6 6]' %vector 2
e=0.1 %error
u(u<=v+e & u>=v-e) %values in vector 2 that are inside the limits
%it only compares values in the same index number
  1 件のコメント
Manjur Hossain
Manjur Hossain 2021 年 5 月 7 日
Thank you Mr. Paulo Silva. You have post it at very earlier but I have implemented it today for my own work. Thank you for solution for nearest value.

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


Stefan
Stefan 2011 年 6 月 8 日
Alright, that works great!
Now, I would like to get the index where it found the values.
In the example above that would be the first value in the first column: (1,1)
Instead of returning the actual value that it found, how can I get out the index?
  2 件のコメント
Paulo Silva
Paulo Silva 2011 年 6 月 8 日
use the find function
doc find
Jan
Jan 2011 年 6 月 8 日
"L = abs(v - u)<=e" is the logical index, "find(L)" is the numerical index.
This is explained in the "Getting Started" chapters of the documentation.

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


Stefan
Stefan 2011 年 6 月 8 日
You guys are awesome! Thanks a lot!

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by