how to write correct looping

2 views (last 30 days)
vaya putra
vaya putra on 8 Jul 2020
Commented: Walter Roberson on 8 Jul 2020
hi all
i want to read file and which is some condition
for data 1-100 is read properly
but i am confused why is the value in line X>99 i can not get rigth value
A=readfile('pf_ext_pcs_1_ts_1_t_1_000000_0.vtu')';
C = A(10217:13618);
C=cellfun(@(x)sscanf(x,'%f'),C,'UniformOutput',false);
format long g
pf=cell2mat(C); % 0 represents fracture occurs
pf(pf<0.1) =10000; %
km=nan(1000,1); % total cell permeability multiplier
a=1;
for x=1:10000
km(x,:)=1/8*(pf(x,1)+pf(x+1,1)+pf(200+a,1)+pf(201+a,1)+pf(10201+a,1)+pf(10202+a,1)+pf(a+10401,1)+pf(a+10402,1));
if x>99 &&x<199
km(x,:)=1/8*(pf(201-a,1)+pf(202-a,1)+pf(302-a,1)+pf(303-a,1)+pf(10402-a,1)+pf(10403-a,1)+pf(10503-a,1)+pf(10504-a,1));
elseif x > 199
km(x,:)=1/8*(pf(302-a,1)+pf(303-a,1)+pf(403-a,1)+pf(404-a,1)+pf(10503-a,1)+pf(10504-a,1)+pf(10604-a,1)+pf(10605-a,1));
elseif x > 299
km(x,:)=1/8*(pf(403-a,1)+pf(404-a,1)+pf(504-a,1)+pf(505-a,1)+pf(10604-a,1)+pf(10605-a,1)+pf(10705-a,1)+pf(10706-a,1));

Accepted Answer

Florian
Florian on 8 Jul 2020
for x=1:10000
if x<=99
km(x,:)=1/8*(pf(x,1)+pf(x+1,1)+pf(200+a,1)+pf(201+a,1)+pf(10201+a,1)+pf(10202+a,1)+pf(a+10401,1)+pf(a+10402,1));
elseif x>99 && x<=199
km(x,:)=1/8*(pf(201-a,1)+pf(202-a,1)+pf(302-a,1)+pf(303-a,1)+pf(10402-a,1)+pf(10403-a,1)+pf(10503-a,1)+pf(10504-a,1));
elseif x>199 && x<=299
km(x,:)=1/8*(pf(302-a,1)+pf(303-a,1)+pf(403-a,1)+pf(404-a,1)+pf(10503-a,1)+pf(10504-a,1)+pf(10604-a,1)+pf(10605-a,1));
else
km(x,:)=1/8*(pf(403-a,1)+pf(404-a,1)+pf(504-a,1)+pf(505-a,1)+pf(10604-a,1)+pf(10605-a,1)+pf(10705-a,1)+pf(10706-a,1));
end
end
  1 Comment
Walter Roberson
Walter Roberson on 8 Jul 2020
for x=1:10000
if x<=99
km(x,:)=1/8*(pf(x,1)+pf(x+1,1)+pf(200+a,1)+pf(201+a,1)+pf(10201+a,1)+pf(10202+a,1)+pf(a+10401,1)+pf(a+10402,1));
elseif x<=199
km(x,:)=1/8*(pf(201-a,1)+pf(202-a,1)+pf(302-a,1)+pf(303-a,1)+pf(10402-a,1)+pf(10403-a,1)+pf(10503-a,1)+pf(10504-a,1));
elseif x<=299
km(x,:)=1/8*(pf(302-a,1)+pf(303-a,1)+pf(403-a,1)+pf(404-a,1)+pf(10503-a,1)+pf(10504-a,1)+pf(10604-a,1)+pf(10605-a,1));
else
km(x,:)=1/8*(pf(403-a,1)+pf(404-a,1)+pf(504-a,1)+pf(505-a,1)+pf(10604-a,1)+pf(10605-a,1)+pf(10705-a,1)+pf(10706-a,1));
end
end
You had to fail x<=99 to reach the first elseif . There are several ways to fail x<=99:
  1. x is non-scalar and has a mix of values. However, in a for x=1:10000 you can be sure that x will be numeric scalar
  2. x is nan, because comparing nan to anything is false, including nan == nan is false as is nan ~= nan. But we are in a for x = integer vector loop, so we know x will not be nan
  3. x <= 99 fails because x is a numeric scalar that is > 99
We can rule out the first two cases by context, leaving x > 99 . So the first elseif will only be reached if x > 99. And that being the case, it is not productive to test x>99 in the elseif: if it were false, then we could not have reached the elseif (under the pre-condition of integer scalar for loop)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!