Last change
on this file since 177 was
37,
checked in by (none), 15 years ago
|
Added original make3d
|
-
Property svn:executable set to
*
|
File size:
1.4 KB
|
Rev | Line | |
---|
[37] | 1 | function e = maxdiff(a,b,rel)
|
---|
| 2 | % MAXDIFF(A,B) returns the maximum difference in any field or element.
|
---|
| 3 | % Matching infinities or NaNs do not count.
|
---|
| 4 | %
|
---|
| 5 | % MAXDIFF(A,B,REL) measures the per-element relative difference (A-B)/(REL + A)
|
---|
| 6 | %
|
---|
| 7 | % Examples:
|
---|
| 8 | % maxdiff([1 2 3 nan inf -inf],[1 2 4 nan inf -inf]) % = 1
|
---|
| 9 |
|
---|
| 10 | % Written by Tom Minka
|
---|
| 11 | % (c) Microsoft Corporation. All rights reserved.
|
---|
| 12 |
|
---|
| 13 | if nargin < 3
|
---|
| 14 | rel = [];
|
---|
| 15 | end
|
---|
| 16 |
|
---|
| 17 | e = 0;
|
---|
| 18 | if ~isequal(class(a), class(b))
|
---|
| 19 | fprintf('maxdiff: incompatible types\n');
|
---|
| 20 | e = Inf;
|
---|
| 21 | return
|
---|
| 22 | end
|
---|
| 23 | if isa(a,'struct')
|
---|
| 24 | for f = fieldnames(a)'
|
---|
| 25 | field = char(f);
|
---|
| 26 | if ~isfield(b,field)
|
---|
| 27 | fprintf('maxdiff: second argument lacks field %s\n', field);
|
---|
| 28 | e = Inf;
|
---|
| 29 | return
|
---|
| 30 | end
|
---|
| 31 | e = max(e,maxdiff(a.(field), b.(field), rel));
|
---|
| 32 | end
|
---|
| 33 | return
|
---|
| 34 | end
|
---|
| 35 | if ~isequal(size(a),size(b))
|
---|
| 36 | fprintf('maxdiff: size mismatch\n');
|
---|
| 37 | e = Inf;
|
---|
| 38 | return
|
---|
| 39 | end
|
---|
| 40 | a = a(:);
|
---|
| 41 | b = b(:);
|
---|
| 42 | if iscell(a)
|
---|
| 43 | for i = 1:numel(a)
|
---|
| 44 | e = max(e,maxdiff(a{i},b{i}));
|
---|
| 45 | end
|
---|
| 46 | return
|
---|
| 47 | end
|
---|
| 48 | i = isnan(a);
|
---|
| 49 | if any(i ~= isnan(b))
|
---|
| 50 | % mismatched NaNs
|
---|
| 51 | e = Inf;
|
---|
| 52 | return
|
---|
| 53 | elseif sum(i) > 0
|
---|
| 54 | a = a(~i);
|
---|
| 55 | b = b(~i);
|
---|
| 56 | end
|
---|
| 57 | i = ~isfinite(a);
|
---|
| 58 | if any(i ~= ~isfinite(b))
|
---|
| 59 | % mismatched infs
|
---|
| 60 | e = Inf;
|
---|
| 61 | return
|
---|
| 62 | elseif ~isequal(a(i),b(i))
|
---|
| 63 | e = Inf;
|
---|
| 64 | return
|
---|
| 65 | else
|
---|
| 66 | a = a(~i);
|
---|
| 67 | b = b(~i);
|
---|
| 68 | end
|
---|
| 69 | if isempty(a)
|
---|
| 70 | e = 0;
|
---|
| 71 | return
|
---|
| 72 | end
|
---|
| 73 | e = abs(a(:) - b(:));
|
---|
| 74 | if ~isempty(rel)
|
---|
| 75 | e = e ./ (rel + abs(a(:)));
|
---|
| 76 | end
|
---|
| 77 | e = max(e);
|
---|
Note: See
TracBrowser
for help on using the repository browser.