That would be a poor choice.

A = 1:4;

sum(A>3)

is a very convenient way to find out how many elements in A are greater than 3. MATLAB has allowed reasonable calculations with logicals for a long time. Taking that out now as a general rule would lead, I think, to incompatibilities that are likely not warranted.

--Loren

]]>I agree about the code analyzer message. We already have that in our enhancement database.

–Loren

]]>–Loren

]]>This type of mistake is about as common as two other, related mistaken uses of MATLAB’s logical operators. The first is when the user doesn’t realize that MATLAB does not use the conditional to filter elements for operation inside the IF statement. For example, I have often seen users do this:

x = 1:5;

if x<=3

x = 10;

end

The user expects this result: x = [10 10 10 4 5]

The reason why the user expects this value is the false understanding that only those elements of x that satisfy the conditional statement are operated on inside the IF statement!

As I mentioned before, this is related to the other common mistake. The MATLAB IF conditional only passes through if all of the elements of a non-empty array evaluate to true. Thus the previous example is equivalent to:

x = 1:5;

if all(x<=3)

x = 10;

end

Also note that the statement inside the IF statement reassigns x to a scalar value of 10; it never operates on any subset of elements. Add to these the way MATLAB treats empty arrays and there is enough material for a long blog post (I know you have covered at least some of these topics before).

]]>That’s how the MATLAB language was designed, without x

–Loren

]]>