if mi(:)>(a1(:)+n(:)) && mi(:)<(a1(:)-n(:)) && (ma(:)>(b1(:)+n(:)) && ma<(b1(:)-n(:))
boxing_out=all(1);
else if mi(:)>(a2(:)+n(:))&&mi(:)<(a2(:)-n(:))&&ma(:)>(b2(:)+n(:))&&ma(:)<(b2(:)-n(:))
boxing_out(:)=2;
else if mi(:)>(a3(:)+n(:))&&mi(:)<(a3(:)-n(:))&&ma(:)>(b3(:)+n(:))&&ma(:)<(b3(:)-n(:))
boxing_out(:)=3;
else if mi>(a4+n)&&mi<(a4-n)&&ma>(b4+n)&&ma<(b4-n)
boxing_out(:)=4;
else if mi>(a5+n)&&mi<(a5-n)&&ma>(b5+n)&&ma<(b5-n)
boxing_out(:)=5;
else
boxing_out(:)=0;
end
end
end
end
end
I write this but I get the follow error. Expected a scalar. Non-scalars are not supported in IF or WHILE statements, or with logical operators. Instead, use ALL to convert matrix logicals to their scalar equivalents. I don't how to solve this question.Who can help me .
[EDITED, Jan, please format your code]

 채택된 답변

Roger Stafford
Roger Stafford 2014년 10월 12일
편집: Roger Stafford 2014년 10월 12일

5 개 추천

Apparently your variables 'mi', 'ma', 'a1', 'b1', ..., are arrays with more than one element - that is, they are not scalars. Consequently you cannot use the "short-circuit" logical operators "&&" and "||" with them. Remember, if a variable 'v' is not a scalar, then neither is 'v(:)'.
Also if you use the 'if' function on a logical vector as you seem to have done, it can only come true if all that vector's elements are simultaneously true in the accompanying expression. I suspect that is not what you intended here.
One further observation. You will find it more convenient to use the 'elseif' construct rather than using separate 'else' and 'if' functions.
One more added observation: Rather than
x > y+z & x < y-z
you can use the equivalent
-z > abs(x-y)

추가 답변 (0개)

질문:

2014년 10월 12일

댓글:

2014년 10월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by