Storing Maximum Value & corresponding variable in iterative process.

1 次查看(过去 30 天)
max1 = [-inf 0 0 0];
for p=1:3
for q=1:6
for z=1:100
if p+q<6;
a=2*p;
b=5*q;
k=z*z;
x=k*4+1;
k1 = a*x+b*k+1;
if k1 > 4,
max(1,z) = k1;
max(2,z) = p;
max(3,z) = q;
max(4,z) = z; end
else
end
end
end
end
Here there are total three loops used for p,q,z. I want to store value pf k1 which is more than 4 & corresponding p,q,z. i am facing problem that while loop is executed values of variable max are getting replaced in every loop. I have to gather data having k1>4 and correponding p,q,z

回答(1 个)

Jan
Jan 2013-3-21
The minimal possible value of k1 is 16, when I set the minimal values for p, q, z in the formula k1 = a*x+b*k+1. Therefore you can omit the check for > 4. And all possible combinations with p+q<6 are wanted as result? This could be achieve much easier.
The indentation of your code is confusing due to the "end" which is append behinde the line.
You pre-allocate the variable max1, but in the code max is assigned, but it leads to problems frequently, when built-in functions are shadowed.
  1 个评论
Shantanu K
Shantanu K 2013-3-21
okay...max1 is mistake of mine.....then how to proceed. This is not actual problem....i have made replication to make it simple. Logic which i will get here will be implemented in main program......in that program its not so easy to tell about minimum value......so how to proceed then?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by