How to solve error :Operands to the || and && operators must be convertible to logical scalar values.

조회 수: 1 (최근 30일)
I have a following simple program as below and I want to test the result
clc;
close all;
a=0.1:0.1:1
b=1.5:.5:5
m=6
if m>a && m>b
print('The place is good')
elseif m<a && m<b
print('The place is bad')
end
Here I want to compare the value of m with all the value of a( ie from 0.1:1) & b(1.5:5). .. Here I want the program to compare with all the values of a and b and get an output. Can somebody help and correct my problem.
Thanks
  댓글 수: 3
s p
s p 2018년 3월 26일
No I want to compare m with all values of a & b, not with only max value of a&b
In this case a= 0.1 0.2 0.3 0.4 ..........1
b=1.5 2 2.5 3 3. 5 ....5
now I want to compare m value with all these value and jump to the conclusion...
I know taking max works for this case but I just wanna know how to make comparison of single value (here m) with the every element of array (here a & b)
Guillaume
Guillaume 2018년 3월 26일
I just wanna know how to make comparison of single value with the every element of array
That is trivial to do. However that gives you a vector of true and false values for each element compared. Given that, what should
if [true true false false]
disp('Am I true or false?');
else
disp('A bit of both!');
end
should display?

댓글을 달려면 로그인하십시오.

답변 (1개)

John Harris
John Harris 2018년 3월 26일
These produce logical arrays.
m>a
m>b
You might be looking for the all() function, which will produce a single logical:
all(m>a)
see also the any() function

카테고리

Help CenterFile Exchange에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by