How to find maximum value for 3 iterative variables.
이전 댓글 표시
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
2013년 3월 7일
Your statement y=a*x+b*k+1; is completely overwriting any previous value of the variable "y", including the use of "y" as an array from the previous iteration of the very next line.
Shantanu K
2013년 3월 7일
편집: Shantanu K
2013년 3월 7일
Walter Roberson
2013년 3월 7일
"y" cannot be simultaneously a scalar and an array. You need to use a different name for one of the two. Or just skip the temporary variable and use
y(p,q,z) = a*x + b*k + 1;
Shantanu K
2013년 3월 7일
편집: Walter Roberson
2013년 3월 7일
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
2013년 3월 7일
채택된 답변
추가 답변 (1개)
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
2013년 3월 7일
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"
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!