Error using && in if conditional statements

2 views (last 30 days)
Camilo Perez
Camilo Perez on 20 Jun 2019
Edited: Matt J on 20 Jun 2019
cont=1;
for x=0:N
for y=0:N
for z=0:N
fh(cont) = ( c/2 ) * ( sqrt((x/lx)^2 + (y/ly)^2 + (z/lz)^2));
cont=cont+1;
end
end
end
Here the frequency for different normal vibration modes is calculated and saved in fh
b1=0;
co=cont;
while co>0
if fh<50 && fh>25
b1=b1+1;
end
co=co-1;
end
but when I try to count how many modes have a frequency between 2 parameters by using &&, I get the following error
Operands to the || and && operators must be convertible to logical scalar values.
Error in ModosNormales (line 91)
if fh<50 && fh>25
I tried the conditional AND, &, ||, but it does not give me the result that should

Answers (1)

Matt J
Matt J on 20 Jun 2019
Edited: Matt J on 20 Jun 2019
I believe your entire code can be replaced with 3 lines,
[x,y,z]=ndgrid(0:N);
fh=( c/2 ) * ( sqrt((x(:)/lx)^2 + (y(:)/ly)^2 + (z(:)/lz)^2));
b1=sum(fh<50 & fh>25)
No need for loops and if statements.
But to answer your question, you are not indexing the vector fh in your loop, so the entire vector quantity fh>50 is being given to the && operator. The && operator does not work with vectors and, even if it did, it's rare that you would want to give a vector expression to an if statement (I've never done it).

Community Treasure Hunt

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

Start Hunting!