Question

I'm checking the objects validity in Matlab with the combination of zeros in the array. I want to use && operator to do so but when I use that in a single if statement, it givs me the error: Operands to the || and && operators must be convertible to logical scalar values.

My Code:

for ii=1:1000
 if (Vec(ii,:) ~= 0) && (isvalid(MyObj))
%opeartions
end
end
Was it helpful?

Solution

Those operators perform shortcut evaluation similar to the comparable C operators. That is a || b only evaluates b of a is false, and a && b only evaluates b if a true. It will not do this shortcut evaluation on a point by point basis.

Since at least one of your operands appears to be a vector of boolean conditions (Vec(ii,:)~=0), Matlab is telling you that the shortcut evaluation cannot be peformed.

You put this in an if, so you needed a single scalar boolean anyway. I would guess that you meant the scalar first operand as any(Vec(ii,:)~=0) rather than the vector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top