http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503
MATLAB Central Newsreader  quick search of a binary array within a binary matrix.
Feed for thread: quick search of a binary array within a binary matrix.
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 30 May 2011 18:51:02 +0000
quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838536
Jon
Dear all<br>
My question is:<br>
I have a binary matrix<br>
B = [1 0 0, 0 1 1, 0 0 1, 1 1 0];<br>
Besides, I have a binary vecor<br>
b = [1 0 1]<br>
Is there any efficient way to check if b is contained in B? is it an advantage to have only binary numbers? And the fact that I dont want to know the exact position but only if it is contained? can be done by indexing?<br>
<br>
In the real case, b is made up by 3000 elements so indexing may not be possible.<br>
<br>
Thank you very much in advance.

Mon, 30 May 2011 19:19:07 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838538
dm
"Jon" wrote in message <is0oum$e82$1@newscl01ah.mathworks.com>...<br>
> Dear all<br>
> My question is:<br>
> I have a binary matrix<br>
> B = [1 0 0, 0 1 1, 0 0 1, 1 1 0];<br>
> Besides, I have a binary vecor<br>
> b = [1 0 1]<br>
> Is there any efficient way to check if b is contained in B? is it an advantage to have only binary numbers? And the fact that I dont want to know the exact position but only if it is contained? can be done by indexing?<br>
> <br>
> In the real case, b is made up by 3000 elements so indexing may not be possible.<br>
> <br>
> Thank you very much in advance.<br>
<br>
Probably not the most compact solution, but works:<br>
<br>
B = [1 0 0; 0 1 1; 0 0 1; 1 1 0; 1 0 1];<br>
b = [1 0 1];<br>
[M N] = size(B);<br>
C = repmat(b,M,1)<br>
t = sum(~xor(B,C),2);<br>
if ~isempty(find(t==numel(b), 1))<br>
isMem = 1<br>
end

Mon, 30 May 2011 19:40:23 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838541
ImageAnalyst
Did you look at bwhitmiss() in the Image Processing Toolbox?<br>
<br>
Or else try this:<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/23998findsubmat">http://www.mathworks.com/matlabcentral/fileexchange/23998findsubmat</a>

Mon, 30 May 2011 20:27:04 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838550
Jon
Thank you very much for your rapid response!<br>
<br>
There is not any special function to do that? I mean, doing<br>
tmp=find(b*B'==sum(b));<br>
is usually faster, rigth?<br>
<br>
Thank you again!

Mon, 30 May 2011 21:13:55 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838556
ImageAnalyst
How much speed do you need? Do you really think you'll notice any<br>
time difference for such tiny arrays of 3000 elements or less?

Mon, 30 May 2011 22:27:52 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838576
ImageAnalyst
You had a slight error in your definition of B  commas instead of<br>
semicolons. I also changed b so that there was a match.<br>
Corrected version:<br>
<br>
B = [1 0 0; 0 1 1; 0 0 1; 1 1 0]<br>
b = [1 1 0]<br>
matchingRow = find(b*B' == sum(b))<br>
<br>
That works fine, so if you want that, that's fine, and it doesn't<br>
require any toolbox functions.

Mon, 30 May 2011 22:38:02 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838579
Roger Stafford
"Jon" wrote in message <is0oum$e82$1@newscl01ah.mathworks.com>...<br>
> Dear all<br>
> My question is:<br>
> I have a binary matrix<br>
> B = [1 0 0, 0 1 1, 0 0 1, 1 1 0];<br>
> Besides, I have a binary vecor<br>
> b = [1 0 1]<br>
> Is there any efficient way to check if b is contained in B? is it an advantage to have only binary numbers? And the fact that I dont want to know the exact position but only if it is contained? can be done by indexing?<br>
> <br>
> In the real case, b is made up by 3000 elements so indexing may not be possible.<br>
> <br>
> Thank you very much in advance.<br>
<br>
"Jon" wrote in message <is0uio$r70$1@newscl01ah.mathworks.com>...<br>
> Thank you very much for your rapid response!<br>
> <br>
> There is not any special function to do that? I mean, doing<br>
> tmp=find(b*B'==sum(b));<br>
> is usually faster, rigth?<br>
> <br>
> Thank you again!<br>
        <br>
It is not clear what you are asking where you write<br>
<br>
B = [1 0 0, 0 1 1, 0 0 1, 1 1 0];<br>
<br>
You called it a matrix but, as it stands, it is a 1 by 12 vector. If this is really what you mean, then are you asking whether b = [1 0 1] is one of the ten vectors within B: [1 0 0], [0 0 0], [0 0 1], [0 1 1], [1 1 0], [1 0 0], [0 0 1], [0 1 1], [1 1 1], [1 1 0], or are you asking whether b is one of the four vectors [1 0 0], [0 1 1, [0 0 1], [1 1 0]?<br>
<br>
If the latter is what you mean, then turn B into an actual matrix:<br>
<br>
B = [1 0 0; 0 1 1; 0 0 1; 1 1 0];<br>
<br>
and use 'ismember' with the 'rows' option:<br>
<br>
t = any(ismember(b,B,'rows'));<br>
<br>
If it is the former meaning then you should use the 'hankel' function on the vector version of B before using 'ismember'.<br>
<br>
One thing is certain. The code<br>
<br>
tmp=find(b*B'==sum(b));<br>
<br>
will not get the answer you seem to be asking for by an conceivable stretch of the imagination.<br>
<br>
Roger Stafford

Tue, 31 May 2011 01:40:05 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838598
Roger Stafford
"Roger Stafford" wrote in message <is168a$fk2$1@newscl01ah.mathworks.com>...<br>
> ..........<br>
> One thing is certain. The code<br>
> <br>
> tmp=find(b*B'==sum(b));<br>
> <br>
> will not get the answer you seem to be asking for by an conceivable stretch of the imagination.<br>
> <br>
> Roger Stafford<br>
        <br>
It has occurred to me that by "b is contained in B" you might have meant that at least one of the rows of B (after being converted to a matrix) has the property that each of its binary bits is a 1 wherever the corresponding b vector has a 1  that is, that a 1 denotes membership in the set of possible columns indices  rather than that the two rows are bitforbit equal. If the former was your meaning of 'contained in', then the matrix multiplication you mentioned would indeed work properly.<br>
<br>
My comment about that is twofold. I think you should have used a better way of wording your problem than just "contained in", and I needed a greater "stretch of the imagination".<br>
<br>
Roger Stafford

Tue, 31 May 2011 13:40:44 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838698
Steven_Lord
<br>
<br>
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in <br>
message news:is1gtl$ao4$1@newscl01ah.mathworks.com...<br>
> "Roger Stafford" wrote in message <br>
> <is168a$fk2$1@newscl01ah.mathworks.com>...<br>
>> ..........<br>
>> One thing is certain. The code<br>
>><br>
>> tmp=find(b*B'==sum(b));<br>
>><br>
>> will not get the answer you seem to be asking for by an conceivable <br>
>> stretch of the imagination.<br>
>><br>
>> Roger Stafford<br>
>         <br>
> It has occurred to me that by "b is contained in B" you might have meant <br>
> that at least one of the rows of B (after being converted to a matrix) has <br>
> the property that each of its binary bits is a 1 wherever the <br>
> corresponding b vector has a 1  that is, that a 1 denotes membership in <br>
> the set of possible columns indices  rather than that the two rows are <br>
> bitforbit equal. If the former was your meaning of 'contained in', then <br>
> the matrix multiplication you mentioned would indeed work properly.<br>
<br>
If that's the meaning the OP intended, they can look at the ISMEMBER <br>
function with the 'rows' flag.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Tue, 31 May 2011 17:04:04 +0000
Re: quick search of a binary array within a binary matrix.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308503#838733
Roger Stafford
"Steven_Lord" <slord@mathworks.com> wrote in message <is2r4s$nov$1@newscl01ah.mathworks.com>...<br>
> If that's the meaning the OP intended, they can look at the ISMEMBER <br>
> function with the 'rows' flag.<br>
         <br>
Steve, I don't think 'ismember' with 'rows' option would be appropriate with that meaning of "contained in" when the solution<br>
<br>
any(b*B'==sum(b))<br>
<br>
is so much more efficient. As I pointed out earlier in this thread, 'ismember' should be used with the other meaning.<br>
<br>
Roger Stafford