フィルターのクリア

For loop repetition question

1 回表示 (過去 30 日間)
Todd
Todd 2013 年 11 月 29 日
回答済み: Roger Stafford 2013 年 11 月 29 日
Hey guys, I'm trying to make my code below to give me N=1:10 (repeating the for loop 10 times) in addition to 1:20 and 1:50, do I need to make separate for loops for this or is there some way to get the current loop to give me this information?
clear;
x0=[42;0;95];
extinct=0;
W=zeros(1,200);
for N=[1:10]
F=2*rand(1)
for t=1:200;
W(t)=F;
A=[0 0 F; .6 0 0; 0 .75 .55];
x=(A^t)*x0;
if (sum(x)<1);
extinct=extinct+1;
break;
end
end
end
fprintf('Number of extinction events:%d\n',extinct)

採用された回答

Image Analyst
Image Analyst 2013 年 11 月 29 日
You don't need the brackets, by the way. You'd need to wrap that whole loop in another loop to handle the other top values of N:
upperN = [10,20,50]
for k = upperN
fprintf('Upper N = %d\n', k);
for n = 1 : k
fprintf(' n = %d\n', n);
end
end

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2013 年 11 月 29 日
Are you certain you want matrix power in A^t rather than element-wise power as in A.^t? Either way, your coding can be made more efficient in my opinion. Also what is the point of W(t) here. It gets repeatedly erased in subsequent passes through the outer loop - that is, for each subsequent value of N the array W is overwritten, or at least partially so.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by