bourassa <email@example.com> writes:
> Hi there. Here is a snippet of code that creates binary matrices and
> checks to see if they have an inverse that is also binary. I use
> "ismember" to verify that each element in the inverse is either 0 or
> 1. Unfortunately, the "while" loop exits immedeately. Why?
> while ismember(inverse,set)~=ones(size(ismember(inverse,set)))
It often helps to step through the code by hand and look at the result
of various expressions. Run the first 3 lines, then:
OK, makes sense. Except I see some elements of 0.0000 that don't
register as 0. More on this later.
Nothing to see here.
OK, notice this returns a matrix of 0's and 1's (it's also the binary
inverse of the ismember result). What happens when you use a matrix
as a condition for a while loop? Right, there's an implicit "all" in
there. So you need a scalar instead. If you use "isequal", each
element must be equal for it to return a 1.
BUT you can skip the ones() call, since you want to find the case that
ismember returns all ones. So, use: all(all(ismember(inverse,set)))
for the condition.
BUT notice that some elements very close to 0 and 1 will not be equal,
due to roundoff error. You might instead want to use a tolerance,
min(abs(inverse-1), abs(inverse)) < 1e-10
Peter Boettcher <firstname.lastname@example.org>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/