How to find maximum value for 3 iterative variables.

1 回表示 (過去 30 日間)
Shantanu K
Shantanu K 2013 年 3 月 7 日
In this i have to do following operation mentioned:
Here value of a,b,z are changing continuously. For every value program will calculate value of 'y'.
It will become three dimensional array.
For entire these iterations i have to find out maximum value of 'y' and corresponding value of 'a' & 'b'.
I am facing one more problem, when i formulated array it was showing lot more zeros.
for p=1:6
a=2*p;
for q=1:6
b=5*q;
for z=1:100
k=z*z;
x=k*4+1;
y=a*x+b*k+1;
y(p,q,z)=y;
end
end
end
  6 件のコメント
Walter Roberson
Walter Roberson 2013 年 3 月 7 日
Do you want the 'a' and 'b' (as you wrote the first time) or the 'p' and 'q' (as you write now) ?
Shantanu K
Shantanu K 2013 年 3 月 7 日
Thanks Walter for your help !

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 3 月 7 日
編集済み: Andrei Bobrov 2013 年 3 月 7 日
max1 = [-inf 0 0 0];
for p=1:6
for q=1:6
for z=1:100
a=2*p;
b=5*q;
k=z*z;
x=k*4+1;
k1 = a*x+b*k+1;
y(p,q,z) = k1;
if k1 > max1(1), max1 = [k1,p,q,z]; end
end
end
end
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 3 月 7 日
Re-using the variable "k" could be confusing to me.
Andrei Bobrov
Andrei Bobrov 2013 年 3 月 7 日
Oh! Typo, thank you, Walter!
Corrected.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 3 月 7 日
Before loop:
maxy = -inf;
a_at_max = -inf;
b_at_max = -inf;
then inside the loop, where you assign into y, test to see if the new value is greater than maxy, and if it is, record the a and b values
  2 件のコメント
Shantanu K
Shantanu K 2013 年 3 月 7 日
I am not able to understand it sir, can you please elaborate?
then inside the loop, where you assign into y, test to see if the new value is greater than maxy, and if it is, record the a and b values This part i am not able to convert to program.
Walter Roberson
Walter Roberson 2013 年 3 月 7 日
if y(p,q,z) > maxy
%then update your record of which "a" and "b" values led to this maximum
end
Oh yes and remember to update maxy with the new maximum as well.
Hint: "update" means "do an assignment to"

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

カテゴリ

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