Matrix betweenness - how to?

Bg: I have three matrices of the same size. A, B and C. Most elements in these matrices are the same except a few.
Ask: I want to deduce a 4th matrix D (the same size) that gives element "1" whenever corresponding element of B is between that of A and C. Otherwise 0 for that element.
Clarification: I don't care too much if it isn't "1" and it is an actual subtraction between element A and C. I just need a 0 if B's element is not between that of A and C.
Preferably I want a matlab/ opencv way to do this.

3 Comments

What have you tried so far?
condition = (A < B && B < C)
I needed element wise comparison and store each element result into answer matrix. So the answer is a matrix of binary results but not a scalar binary itself. Thanks for the comment.
A < B & B < C

Sign in to comment.

Answers (1)

dpb
dpb on 21 Aug 2014
Edited: dpb on 21 Aug 2014
You asked for my utility function iswithin --
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
This version is inclusive...the alternative of isbetween is exclusive--
function flg=isbetween(x,lo,hi)
% returns T for values between range of inputs
% SYNTAX:
% [log] = isbetween(x,lo,hi)
% returns T for x between lo and hi values, exclusive
flg= (x>lo) & (x<hi);
I've yet another that has optional flags for each bound being inclusive or not.
In use it's simply for your case --
d=iswithin(b,a,c);
The assumption is that all(a(:)<=b(:))==TRUE. If your data aren't necessarily ordered (so the comparison could be in either direction), simply use--
d=iswithin(b,min(a,c),max(a,c));
to get the ordering right for the limit arguments LO/HI.

2 Comments

DotFrammie
DotFrammie on 21 Aug 2014
Edited: dpb on 21 Aug 2014
Thanks you dpb very much for the answer!! What if the assumption isn't true and the three matrices are as follows...
Clarification Example: If A, B and C are as follows:
A= [12 24 18; 34 45 35; 08 12 34];
B= [13 26 18; 45 01 38; 80 10 34];
C= [10 34 18; 45 45 35; 90 09 34];
I want to calculate a D using openCV or matrices manipulations so that it can be
D= [0 1 1; 1 0 0; 1 0 1];
dpb
dpb on 21 Aug 2014
Edited: dpb on 21 Aug 2014
OK, you definitely want the iswithin version of inclusive comparison and to use the min()/max() on the two limits arrays as outlined...
>> iswithin(B,min(A,C),max(A,C))
ans =
0 1 1
1 0 0
1 1 1
>>
I think the middle entry, third row in your D is in error, however, as 10 is certainly between 9 and 12.

Sign in to comment.

Categories

Find more on Automotive in Help Center and File Exchange

Asked:

on 21 Aug 2014

Edited:

dpb
on 21 Aug 2014

Community Treasure Hunt

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

Start Hunting!