フィルターのクリア

How to find similar elements with small difference (+/- 5%)?

1 回表示 (過去 30 日間)
Ivan Shorokhov
Ivan Shorokhov 2015 年 7 月 2 日
編集済み: Ivan Shorokhov 2015 年 7 月 2 日
Given: I have 2 arrays. For example: A = [97.2 103.4 109.6]; B = [99.0 105.2 111.4];
Want: I want to find nearest common values with difference +/- 5%. Such as:
D =
A B
------- -------
97.2 99.0
103.4 105.2
109.6 111.4
Currently done:
A = [97.2; 103.4; 109.6];
B = [99.0; 105.2; 111.4];
[E,IA,IB] = intersect(round(A.*0.1),round(B.*0.1),'sorted');
D = [A(IA) B(IB)];
Here is the output:
D =
A B
------- -------
97.2000 99.0000
109.6000 105.2000
As you can see I lost 2 values, and last row is also messed up.
Needed: So I'm wondering, if there are any other functions or code I could use to find similar elements with small difference (+/- 5%).
[ACKNOWLEDGMENTS]
Thanks you: Thorsten,Jan Simon,
I will vote for all your answers.
[MATLAB version]
R2014a

採用された回答

Thorsten
Thorsten 2015 年 7 月 2 日
One way would be to do this in a straight-forward loop:
p = 0.05; % percentage of difference accepted
C = nan(1, numel(A));
for i = 1:numel(A)
[d ind] = min(abs(B - A(i)));
if d < abs(p*A(i)), C(i) = B(ind(1)); end
end
D = [A' C'];
  1 件のコメント
Ivan Shorokhov
Ivan Shorokhov 2015 年 7 月 2 日
編集済み: Ivan Shorokhov 2015 年 7 月 2 日
@Thorsten
Thank you for the great solution, it seems to be working fine for this particular case. I will test it larger data-set and will let you know, whether it works or not.
And there is small mistake in the last line. It should be:
D = [A C'];

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

その他の回答 (1 件)

Jan
Jan 2015 年 7 月 2 日
編集済み: Jan 2015 年 7 月 2 日
What about the new function ismembertol ?
[EDITED] And for older Matlab versions: FEX:ismemberf
  1 件のコメント
Ivan Shorokhov
Ivan Shorokhov 2015 年 7 月 2 日
編集済み: Ivan Shorokhov 2015 年 7 月 2 日
Thank you, I will try it out!

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

カテゴリ

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