how to write correct looping

조회 수: 2 (최근 30일)
vaya putra
vaya putra 2020년 7월 8일
댓글: Walter Roberson 2020년 7월 8일
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));

채택된 답변

Florian
Florian 2020년 7월 8일
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
Walter Roberson
Walter Roberson 2020년 7월 8일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by