Storing Maximum Value & corresponding variable in iterative process.

1 view (last 30 days)
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

Answers (1)

Jan
Jan on 21 Mar 2013
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 Comment
Shantanu K
Shantanu K on 21 Mar 2013
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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!