if elseif statement operands

2 views (last 30 days)
Dominika
Dominika on 1 Apr 2014
Commented: Dominika on 1 Apr 2014
Hi,
I have a problem with statement if else, it doesn't work. Error:
Operands to the and && operators must be convertible to logical scalar values.
Error in My_Project (line 31) elseif ((f0<=f)&&(f<fl));
%%My_Project
f = [31.5 40 50 63 80 100 125 160 200 250 315 400 500 630 800....
1000 1250 1600 2000 2500 3150 4000 5000 6300 8000];
f0=220;
fl=2.58;
if (f<f0)
TL=TLm;
elseif ((f0<=f)&&(f<fl));
TL=TLm1+TLm2+20.*log10(f.*d)-29;
else
TL=TLm1+TLm2+6;
end
Thank you,
Dominika

Answers (2)

nl2605
nl2605 on 1 Apr 2014
You have to give a condition for elseif. Something like
elseif ((f0<=f) && (f<f1)== 1) or zero according to your condition.

Jos (10584)
Jos (10584) on 1 Apr 2014
f is an array with multiple elements, and f0 is a scalar (single element). The statement " f < f0 " returns an array the same size as f with true and false values for each element of f. You can use the functions ALL or ANY to convert this to a single value, so it can be used in an IF condition.
However, I suspect that you want to create a new array TL that has the same number of elements as f, but some elements of TL are calculated one way and some another way ...
You can try this
% Assuming TLm, TLm1, TLm2, and d are scalars!
TL = repmat(TLm1+TLm2+6, size(f)) % default values
q1 = (f0<=f) & (f<fl) % condition 1
TL(q1) = TLm1 + TLm2 + 20.*log10(f(q1).*d)-29
q2 = f < f0 % condition 2
TL(q2) = TLm
  2 Comments
Dominika
Dominika on 1 Apr 2014
Thanks for the answer
However, using your code I get TL array with dimentions 1x625 instead of 1x25
Dominika
Dominika on 1 Apr 2014
OK, I got it
Maybe it will be usuful for someone
I dealt with the problem using the following code:
%Input
d=0.25; % separation of two panels [m]
m1=6000; % density of panel 1 [kg/m^3]
m2=8000; % density of panel 2 [kg/m^3]
m=m1+m2;
me=2*m1*m2/(m1+m2); % [kg/m^3]
f = [31.5 40 50 63 80 100 125 160 200 250 315 400 500 630 800....
1000 1250 1600 2000 2500 3150 4000 5000 6300 8000]; % frequencies third octave band
f0=113./sqrt(me.*d); % resonance frequency
fl=55/d;
% Mass law transmission loss
TLm=20.*log10(m.*f)-48; % total construction [dB]
TLm1=20.*log10(m1.*f)-48; % panel 1 [dB]
TLm2=20.*log10(m2.*f)-48; % panel 2 [dB]
%Transmission loss calculation
TL = repmat(TLm1+TLm2+6,1); % default values (TLm1+TLm2+6)
q1 = (f0<=f) & (f<fl); % condition 1
TL(q1) = 20.*log10(m1.*f(q1))-48 + 20.*log10(m2.*f(q1))-48 + 20.*log10(f(q1).*d)-29; % (TLm1+TLm2+20log(fd)-29)
q2 = f < f0; % condition 2
TL(q2) = 20.*log10(m.*f(q2))-48; % (TLm)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!