syntax error on a while loop

조회 수: 11(최근 30일)
Adam 2014년 5월 26일
편집: Adam 2014년 5월 26일
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
while (DC1(1,j) ~= min(DC1(:,450:(c-1))),DC1(1,j) && j<=Xep);
Xbp = j;
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;
Does someone know how I can correct this syntax problem? Thanks


Geoff Hayes
Geoff Hayes 2014년 5월 26일
편집: Geoff Hayes 2014년 5월 26일
The bug in the code is at line:
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!

Community Treasure Hunt

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

Start Hunting!

Translated by