How apply more than one "if" to determine different changes in one "matrix"

1 view (last 30 days)
Hi guys!
I want apply some conditions to determine my "data", starting from a "base" file. I tried this:
for i=1:length(base)
if isfinite(base(i))<=24.2;
data(i)=1.5393*base(i)-13.2;
if isfinite(base(i))>=24.8;
data(i,4)=1.3775*base(i)-9.93;
if isfinite(base(i))>24.2 & isfinite(base(i))<24.8;
data(i,4)=1.1147*homei(i,4)-3.184;
end
end
end
end
But only the first condition was resolved and i don't know what to do to resolve the others two.
Thank you already,
Gustavo

Accepted Answer

dpb
dpb on 18 May 2014
for i=1:length(base)
if isfinite(base(i))<=24.2;
data(i)=1.5393*base(i)-13.2;
if isfinite(base(i))>=24.8;
data(i,4)=1.3775*base(i)-9.93;
if isfinite(base(i))>24.2 & isfinite(base(i))<24.8;
data(i,4)=1.1147*homei(i,4)-3.184;
Since isfinite is a logical, it's output is either 0 or 1 so only the first case would ever be true regardless of the value of base. Eliminate it entirely, then use Matlab vectorized logical addressing to modify the areas of interest.
data=base; % create the second array
ix1=base<=24.2); % logic vector for condition 1
data(ix1)=1.5393*base(ix1)-13.2; % set data for those
ix2=base>=24.8; % and condition 2
data(ix2)=1.3775*base(ix2)-9.93;
ix=~(ix1 | ix2); % the inbetweens are those not >= or <=
data(ix)=1.1147*base(ix)-3.184;

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 18 May 2014
Edited: Azzi Abdelmalek on 18 May 2014
for i=1:length(base)
p=base(i)
q=isfinite(p)
if q<=24.2;
data(i)=1.5393*p-13.2;
elseif q>=24.8;
data(i,4)=1.3775*p-9.93;
elseif q>24.2 & q<24.8;
data(i,4)=1.1147*homei(i,4)-3.184;
end
end

Tags

Community Treasure Hunt

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

Start Hunting!