Storing Maximum Value & corresponding variable in iterative process.

1 Ansicht (letzte 30 Tage)
Shantanu K
Shantanu K am 21 Mär. 2013
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

Antworten (1)

Jan
Jan am 21 Mär. 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 Kommentar
Shantanu K
Shantanu K am 21 Mär. 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by