why if elseif statement is not working
Show older comments
Hi,
I have this following code and attached data. only if statement is working but not the rest. can any please help?
clc
clear all
numdata = xlsread('data.xlsx');
va=numdata(:,1);
vb=numdata(:,2);
vc=numdata(:,3);
for i=1:length(va)
if((va(i)>207 | va(i) <253) &(vb(i)>207 | vb(i)<253) & (vc(i)>207 | vc(i)<253))
ma(i)=1;
mb(i)=1;
mc(i)=1;
elseif ((va(i)>253 | va(i) <260)&(vb(i)>253 | vb(i)<260)&(vc(i)>253 | vc(i)<260))
ma(i)=(260-va(i))/(260-253)
mb(i)=(260-vb(i))/(260-253);
mc(i)=(260-vc(i))/(260-253);
elseif ((va(i)>260)&& (vb(i)>260)&& (vc(i)>260))
ma(i)=0;
mb(i)=0;
mc(i)=0;
end
pmax=5000;
pa(i)=ma(i).*pmax;
pb(i)=mb(i).*pmax;
pc(i)=mc(i).*pmax;
plot(pa)
end
Accepted Answer
More Answers (1)
It's doing exactly what you're telling it to do. Look at the range of the data. Look at the logical tests you're doing. Case 1 and 2 are always true.
I'm assuming this is closer to what you're trying to do:
pmax = 5000;
% v contains all three vectors [va vb vc]
v = xlsread('data.xlsx');
th = [207 253 260];
mask(:,1) = all(v>=th(1) & v<th(2),2);
mask(:,2) = all(v>=th(2) & v<th(3),2);
mask(:,3) = all(v>=th(3),2);
% m contains all three vectors [ma mb mc]
m = zeros(size(v,1),3);
m(mask(:,1),:) = 1;
m(mask(:,2),:) = (th(3)-v(mask(:,2),:))/(th(3)-th(2));
m(mask(:,3),:) = 0; % possibly redundant
% p contains all three vectors [pa pb pc]
p = m*pmax;
plot(p)
Otherwise, you'll have to clarify what the logical comparisons should be.
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
