Compare two or more cell arrays with a specific tolerence?

5 ビュー (過去 30 日間)
Najmeh Eskandari
Najmeh Eskandari 2019 年 2 月 18 日
コメント済み: Rik 2019 年 2 月 20 日
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 件のコメント
Najmeh Eskandari
Najmeh Eskandari 2019 年 2 月 20 日
suppose that i use ismembertol:
ismembertol([0,1],[0,1.01],1e-02)
logical
1 1
ismembertol([0,0,2],[0,0,2],1e-02)
logical
1 1 1
in this case i want to get result 1 but forexample in case:
ismembertol([0,1],[0,1.5],1e-02)
logical
1 0
ismebertol([0,0,2],[0,0,2],1e-02)
logical
1 1
the final result that i want is:
0
I have several cell arrays with such matrices and want to compare the arrays. If in the way that i described the matrices are equal the result shuld be 1.
-The absolute error is my concern.
Jan
Jan 2019 年 2 月 20 日
編集済み: Jan 2019 年 2 月 20 日
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.

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

回答 (1 件)

Rik
Rik 2019 年 2 月 20 日
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 件のコメント
Jan
Jan 2019 年 2 月 20 日
編集済み: Jan 2019 年 2 月 20 日
@Rik: I'm dissapointed by the way ismembertol applies the tolerances. See my comment above:
abs(u-v) <= tol*max(abs([A(:);B(:)]))
So in ismembertol([1, 900], [20, 1000], 1) the tolerance 1 is multiplied by 1000, the maximum element, such that [1,1] is replied.
In addition the OP did not clarify yet, if the order of the elements matters or not.
Rik
Rik 2019 年 2 月 20 日
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.

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

カテゴリ

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