Reputation: 33
Lets say I've two matrices A and x:
A=[10,10;
20,20;
30,30;
NaN,NaN
NaN,NaN];
x=[10,9;
32,25;
19,21;
11,10;
NaN,NaN];
I want to compare both matrices and find rows of x that correspond to any row of A within a certain tolerance of +-0.01 (=+-10%). So in this case:
Row 1 of x matches a row (row 1) in A that is within the tolerance.
Row 2 of x nearly matches a row (row 2) in A, but it is outside of +- 10%.
Row 3 of x matches a row (row 2) in A that is within the tolerance.
Row 4 of x matches a row (row 1 again) in A within the tolerance.
Row 5 of x can be ignored (only NaN).
It is guaranteed that A and x have the same size and number of elements. However, the rows are not in order, so I can't compare row-by-row. I just want to know if a row in x matches ANY row in A and if so, I'd like to save the index of that row. So in my case I'd prefer to have an output (logical) vector like:
v=[1,0,1,1,0]; % Logical vector with matching rows of x
id=find(v==1); % Result is [1,3,4], finds the indices
I tried various combinations of
ismembertol(A,x,0.1) % and ismembertol(A,x,0.1,'ByRows',true)
by using 'find', 'any', 'all' but I can't figure out the solution.
Upvotes: 1
Views: 593
Reputation: 112659
You can use ismembertol
with the 'ByRows'
option:
result = ismembertol(x, A, 0.1, 'ByRows', true);
Note that:
NaN
values cause a row to not match any other row.tol
is interpreted as a fraction of tol*max(abs([A(:); x(:)]))
.Upvotes: 1