MATLAB Answers

Compare two or more cell arrays with a specific tolerence?

27 views (last 30 days)
Najmeh Eskandari
Najmeh Eskandari on 18 Feb 2019
Commented: Rik on 20 Feb 2019
Hi.
I have a cell array with length 3 :
M_t={{[0,1],[0 0 2]},{[0,1.01],[0 0 2]},{[0,1],[0 .001 2]}};
I want to compare these 3 arrays with a specific tolerence. How can i do this?
I have tried intersect before.
Thanks.
  7 Comments
Jan
Jan on 20 Feb 2019
In the case
ismebertol([0,0,2],[0,0,2],1e-02)
the output has 3 elements, not "logical1 1". Is this a typo?
ismembertol uses a strange scaling: for the elements u of the array A and the elements v of the array B:
abs(u-v) <= tol*max(abs([A(:);B(:)]))
This means, that the tolerance is multiplied my the maximum absolute value of the arrays. I do not know a case, where this is really useful.
ismembertol([1, 900], [20, 1000], 1)
Is the result [true, true] expected?!
For all of my cases, ismembertol did not offer a useful method to apply the tolerence. What a pity.
By the way, you still did not mention, which kind of comparison you exactly want. "The absolute error" is not a unique definition. It can mean:
all(abs(a(:) - b(:)) < tol) % or <=
any(abs(a(:) - b(:)) < tol) % or <=
sum(abs(a(:) - b(:)) < tol) < numel(A) / 2
% or a method, which does not consider the order or the elements
You have mentioned ismembertol, which does not consider the order of elements. So it is not clear, if [1, 0] and [0, 1] should be considered as equal or not.
It is hard to find out, what you exactly want. I've spent some time to ask you specific question, but I do not get clear statements and in consequence I assume, that I cannot help you.

Sign in to comment.

Answers (1)

Rik
Rik on 20 Feb 2019
The code below will iterate through the cell levels. The resulting output is only true if all dimensions are equal and all elements are within tolerance.
M_t={{[0,1],[0 0 2]},{[0,1.01],[0 0 2]},{[0,1],[0 .001 3]}};
clc
assert(ismembertol_nested(M_t(1),M_t(2),0.1),'test failed for equal arrays');
assert(~ismembertol_nested(M_t(1),M_t(3),0.1),'test failed for unequal arrays');
assert(~ismembertol_nested(M_t(1),M_t{2},0.1),'test failed for unequal inputs');
function tf=ismembertol_nested(A,B,tol)
%Un-nest cell arrays until ismembertol can be used on the inner arrays.
%This will return false instead of an error if the shapes are not equal on
%any level.
try
if isa(A,'cell')
tol_cell=num2cell(repmat(tol,size(A)));
tf=all(cellfun(@ismembertol_nested,A,B,tol_cell));
else
ia=ismembertol(A,B,tol);
ib=ismembertol(B,A,tol);
tf=all([ia,ib]);
end
catch
tf=false;
end
end
  2 Comments
Rik
Rik on 20 Feb 2019
That seems strange indeed. I think it would make much more sense if that scale factor would be 1 by default, not that max. So to get the behavior I expected you need to call this
ismembertol(A,B,'DataScale',1)
I can see what the usefullness is if you are using it to check for float rounding errors, but that is not what I suspect most people would be using it for. There doesn't seem to be an easy way to search the FEX for the use of a function, so I can't check that suspicion.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by