why elseif (i==1) && j==m condition went unexecuted?

1 回表示 (過去 30 日間)
ravi shukla
ravi shukla 2020 年 8 月 17 日
編集済み: Jan 2020 年 8 月 18 日
A=zeros(12,1);
n=3;
m=4;
for i=1:n
for j=1:m
if i==1 && j==m
A(1,1)=1;
elseif (i==1) && (j>1 || j<m)
A(2,1)=2;
elseif (i==1) && (j==m)
A(3,1)=3;
end
A

採用された回答

Jan
Jan 2020 年 8 月 17 日
編集済み: Jan 2020 年 8 月 18 日
The condition "i==1 && j==m" appears twice:
if i==1 && j==m % First time
A(1,1)=1;
elseif (i==1) && (j>1 || j<m)
A(2,1)=2;
elseif (i==1) && (j==m) % Second time
A(3,1)=3;
end
If the first condition is met already, further elseif branches are not called anymore. The if block is left directly after the first condition is matching and the corresponding block was executed.
  2 件のコメント
Stephen23
Stephen23 2020 年 8 月 17 日
Welcome back!
Jan
Jan 2020 年 8 月 18 日
Hi Stephen, I thought I give it a try. I still cannot unaccept an answer in Firefox, post an image or remove a flag. The auto-editing inserts an "end" if I type an "if". Most likely I have to accept to be tracked by 13 services including facebook, bing, s3.amazonaws and ads-twitter to use Matlab Answers with its full functionality.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by