create a while loop: while all values in a matrix are positive(code)

1 回表示 (過去 30 日間)
teenwent11
teenwent11 2015 年 10 月 12 日
コメント済み: Manolis Michailidis 2015 年 10 月 12 日
I'm trying to make sure all values in a matrix (the inventory of a shop) are positive and to figure out all possible combinations of three products to maximize profit.
When i run the code below, it always runs infinitely. what's wrong with my loop? i'm running r2014a
maxprof=0 inv=[450;250;800;450;600] tel=[1;1;2;1;2] ster=[1;0;2;1;2] spea=[0;0;1;0;1] while (sum(inv>0))==5 for t=1:100 for s=1:100 for sp=1:100 prof=t*tel*80+s*ster*50+sp*spea*40 inv=[inv-tel*t-ster*s-spea*sp] if prof>maxprof maxprof=prof end end end end end
  1 件のコメント
Manolis Michailidis
Manolis Michailidis 2015 年 10 月 12 日
please edit your code so it will be readable

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

回答 (1 件)

Manolis Michailidis
Manolis Michailidis 2015 年 10 月 12 日
You can use the absolute value abs function , or if there are some non positive, by mistake, you can say
a(a>0);
where a is your matrix , hope this helps.

カテゴリ

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