필터 지우기
필터 지우기

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by