Short Question about Multiple cases in for end

1 回表示 (過去 30 日間)
Fahmy Shandy
Fahmy Shandy 2019 年 12 月 7 日
回答済み: dpb 2019 年 12 月 7 日
I want to make multiple commands in "for loop" like this
for j=1:10 && k=0:9 && m=1:11 && n=1:1
x(j)^k + m -2*n
end
But it's not work. It's not my real question actually (because my script is too long to write). I just simplify my question a bit with different approach. What is the correct script?

採用された回答

dpb
dpb 2019 年 12 月 7 日
Depends upon what you mean to do...if want each combination of the four variables, then you write a set of nested for loops...
for j=1:10
for k=0:9
for m=1:11
for n==1:1
y=x(j)^k + m - 2*n;
end
end
end
end
Of course, for n=1:1 is superfluous; one simply defines n outside the loops for a constant value; one presumes that's not the real case. That leaves one with 10*10*11*1 calculations of y; the allocation of a place to store the results isn't shown above.
ndgrid may be of interest...

その他の回答 (1 件)

カテゴリ

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

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by