필터 지우기
필터 지우기

Using ismembertol for multiple coincidences

조회 수: 4 (최근 30일)
Jose Luis Villaescusa Nadal
Jose Luis Villaescusa Nadal 2022년 11월 2일
편집: Bruno Luong 2022년 11월 2일
I have two vectors, and I'm trying to find ALL coincidences of one on the other within a certain tolerance.
For example:
A = [5 3 4 2]; B = [2 4 4 4 6 8];
I want to obtain a cell array containing on each cell the numbers of all the coincidences with a tolerance of 1 (or more) units. (A = B +- 1)
I have a solution with zero units (A = B), which would look something like this:
tol = 0;
[tf, ia] = ismembertol(B,A,tol,'DataScale',1); % For tol = 0, this is equivalent to using ismember
idx = 1:numel(B);
ib = accumarray(nonzeros(ia), idx(tf), [], @(x){x}) % This gives the cell array
The output is:
ib =
[]
[]
[2 3 4]
[1]
Which is as desired.
If I change the tolerance to 1, the code doesn't work as intended. It outputs instead:
tol = 1
[tf, ia] = ismembertol(B,A,tol,'DataScale',1); % For tolerance = 1, this is equivalent to using ismember
idx = 1:numel(B);
ib = accumarray(nonzeros(ia), idx(tf), [], @(x){x}) % This gives the cell array
ib =
[5]
[2 3 4]
[]
[1]
When I would expect to obtain:
ib =
[2 3 4 5]
[1 2 3 4]
[2 3 4]
[1]
What am I doing wrong? Is there an alternative solution?

채택된 답변

Bruno Luong
Bruno Luong 2022년 11월 2일
Using ismembertol is inappropriate
A = [5 3 4 2]; B = [2 4 4 4 6 8];
tol = 1;
[ib,ia]=find(B'<=A+tol & B'>=A-tol);
c = accumarray(ia,ib,[numel(A) 1],@(x){x'})
c = 4×1 cell array
{[2 3 4 5]} {[1 2 3 4]} {[ 2 3 4]} {[ 1]}
c{:}
ans = 1×4
2 3 4 5
ans = 1×4
1 2 3 4
ans = 1×3
2 3 4
ans = 1
  댓글 수: 3
Jose Luis Villaescusa Nadal
Jose Luis Villaescusa Nadal 2022년 11월 2일
편집: Jose Luis Villaescusa Nadal 2022년 11월 2일
When A and B are long vectors, matlab quickly runs out of memory, since the find creates an array of size [numel(A), numel(B)]. Ismembertol, seems to be able to deal with this.
Do you think there is a way to overcome this with your solution?
Bruno Luong
Bruno Luong 2022년 11월 2일
편집: Bruno Luong 2022년 11월 2일
You could use discretize with edges that are sort(A)-tol and sort(A)+tol instead of find. Details need to be worked out.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by