MATLAB: Operands to the || and && operators must be convertible to logical scalar values.

|| and && operatorsif loop

%Code x=[1.01 ;2.23; 3.456 ;4.11; 5.897 ;6.234; 7.456; 8.567; 9.110; 10.333];
a=0;
if((x>=5.2)&&(x<=8.50))
disp('Inside if loop');
a=a+1;
end
a
I want the no of values greater than some value and less than some value in a 1D array. If i am going for if statement with sysntax if((x(:,1)>=5.2)&&(t(:,1)<=8.50)) i am getting Operands to the and && operators must be convertible to logical scalar values error.
If i use if((x(:,1)>=5.2)&(t(:,1)<=8.50)) flow is not going inside the loop and i am not able to get the count as 3 .

Best Answer

Try this:
x=[1.01 ;2.23; 3.456 ;4.11; 5.897 ;6.234; 7.456; 8.567; 9.110; 10.333];
for k = 1 : length(x)
if x(k) >= 5.2 && x(k) <=8.50
fprintf('x(%d) = %f and is in range so we are inside if block.\n', k, x(k));
end
end
In command window:
x(5) = 5.897000 and is in range so we are inside if block.
x(6) = 6.234000 and is in range so we are inside if block.
x(7) = 7.456000 and is in range so we are inside if block.