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
José-Luis
2014년 8월 21일
What have you tried so far?
condition = (A < B && B < C)
DotFrammie
2014년 8월 21일
A < B & B < C
답변 (1개)
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
DotFrammie
2014년 8월 21일
편집: dpb
2014년 8월 21일
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.
카테고리
도움말 센터 및 File Exchange에서 Automotive에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!