フィルターのクリア

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

3 ビュー (過去 30 日間)
Thayumanavan
Thayumanavan 2014 年 4 月 10 日
回答済み: sai m 2015 年 3 月 3 日
%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 .
  1 件のコメント
Image Analyst
Image Analyst 2014 年 4 月 10 日
You don't have a loop at all! There is no "for" and an "if" block is not a loop.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2014 年 4 月 10 日
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.

その他の回答 (3 件)

Marta Salas
Marta Salas 2014 年 4 月 10 日
編集済み: Marta Salas 2014 年 4 月 10 日
find returns the indexes of the values that holds the condition 5.2=< x >=8.50
index = find(x>=5.2 & x<=8.50)
To know the number of values, you can check the size of index
length(index)
  2 件のコメント
Thayumanavan
Thayumanavan 2014 年 4 月 10 日
Thanks,As per find command I got the count as 3. I also tried for loop as follows:
x=[1.01 ;2.23; 3.456 ;4.11; 5.897 ;6.234; 7.456; 8.567; 9.110; 10.333];
a=0;
arraysize=length(x);
for i=1:arraysize
if ((x(i,1)>=5.2)&&(x(i,1)<=8.50))
a=a+1;
else
disp('not going inside loop');
end
end
disp('the count is');
a
.once again Thanks .
Marta Salas
Marta Salas 2014 年 4 月 10 日
That code works for me.
I would suggest you to use fprintf instead of disp to print comments with variables
fprintf('the count is %d \n',a);

サインインしてコメントする。


Horia
Horia 2014 年 5 月 18 日
Try: if condition 1 do something else if condition 2 do the samething; else do something else break; end; end;

sai m
sai m 2015 年 3 月 3 日
if(get(handles.radiobutton1,'Value') == 0 && get(handles.radiobutton2,'Value') == 0 && get(handles.radiobutton5,'Value') == 0 && get(handles.radiobutton6,'Value' == 0 && get(handles.radiobutton7,'Value') == 0 && get(handles.radiobutton8,'Value') == 0 && get(handles.radiobutton9,'Value') == 0 && get(handles.radiobutton10,'Value') == 0)) errordlg('No option selected','File Error'); %user did not select any radio button, then do this guidata(hObject, handles); end;
error ??? Operands to the and && operators must be convertible to logical scalar values.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by