Skip to content
Snippets Groups Projects
Commit 58f9f4dd authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Improved "between" function's behavior

parent bd4f2884
No related branches found
No related tags found
No related merge requests found
function answer=between(arg,a,b)
function answer = between(arg, a, b)
% FUNCTION BETWEEN logical operation
% function answer=between(arg, a, b)
%
% returns true if arg <= b && arg >= a
% arg may either be a scalar or a matrix of same size like a and b
if size(a) ~= size(b)
mexc = MException('BETWEEN:wrong_input', ...
'input arguments need to be same size. Exiting...');
throw(mexc);
end
if ~isscalar(arg) && any(size(a) ~= size(arg))
mexc = MException('BETWEEN:wrong_input', ...
'arg needs to be either a scalar or a matrix of same size as a and b! Exiting...');
throw(mexc);
end
answer = false(size(a));
if (~all(size(a) == size(b) | size(a) == 1 | size(b) == 1))
error('BETWEEN:wrong_input', ...
'input arguments need to be same size');
end
if (size(arg) == size(a))
for ii = 1:numel(a)
if (arg(ii) <= b(ii)) && (arg(ii) >= a(ii))
answer(ii) = true;
end
if (~isscalar(arg) && ~all(size(a) == size(arg) | size(a) == 1))
error('BETWEEN:wrong_input', ...
'arg needs to be either a scalar or a matrix of same size as a and b!');
end
else
for ii = 1:numel(a)
if (arg <= b(ii)) && (arg >= a(ii))
answer(ii) = true;
end
if (numel(arg) == 1 || all(size(arg) == size(a)))
answer = arg <= b & arg >= a;
else
answer = bsxfun(@and, bsxfun(@le, arg, b), bsxfun(@ge, arg, a));
end
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment