syntax error on a while loop
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi I've got a while loop that looks like this:
if(450<=Xep && Xep<=(c-1)) % c-1 is the max index point in the DC1 array
j=450;
while (DC1(1,j) ~= min(DC1(:,450:(c-1))),DC1(1,j) && j<=Xep);
Xbp = j;
j=j+1;
end
end
and matlab shoots back with this error: "Expression or statement is incorrect--possibly unbalanced (, {, or [."
Before I edited the code I have a different problem and it looked like this:
while DC1(1,j) ~= min(DC1(:,450:(c-1))),DC1(1,j) && j<=Xep;
Xbp = j;
j=j+1;
end
Does someone know how I can correct this syntax problem? Thanks
0 Kommentare
Antworten (1)
Geoff Hayes
am 26 Mai 2014
Bearbeitet: Geoff Hayes
am 26 Mai 2014
The bug in the code is at line:
min(DC1(:,450:(c-1))),DC1(1,j)
with the comma being the culprit. The DC1(:,450:(c-1)) returns an mxn matrix of which you want to find the minimum of. And (I'm kind of guessing here) you want to find the minimum of that value with DC1(1,j). Right now, the ,DC1(1,j) is outside of the min command and so the error is raised. What you could do, since DC1(:,450:(c-1)) is not dependent on j is grab this matrix outside of the while loop and compute its minimum there. Then in the while condition compare that minimum value with your matrix value that is dependent on j:
tmpMtx = DC1(:,450:(c-1));
Now get the minimum of that matrix
minMtxVal = min(tmpMtx(:)); % note the use of the colon as we want the minimum
% over the complete matrix and not the minimum of
% each column
NOTE you should convince yourself that the colon is necessary by re-running the above without it and seeing what happens. At the command window, type help min to get information on what is returned if your input is a vector or (in this case?) a matrix. Or if one input is a vector and the other a scalar (which I think is what would have happened in the original code).
Change your while condition to
while (DC1(1,j) ~= min(minMtxVal,DC1(1,j)) && j<=Xep);
And that should work EXCEPT your while condition will error out if j ever exceeds the number of columns in DC1. If j<=Xep is meant to capture that event, then let that be your first condition ( A ) in the while A && B. Else you will need some additional logic in your while body to handle the case where j>size(DC1,2) (i.e. j is greater than the number of columns in DC1).
Hope the above helps!
0 Kommentare
Siehe auch
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!