MATLAB Answers

Extra term in "min" function in while loop

조회 수: 4(최근 30일)
Adam
Adam 2014년 5월 27일
댓글: Adam 2014년 5월 27일
I was given a piece of code to work on and edit but I'm running into a few errors because I am unsure what this extra piece of term means in the code, the code looks like this:
while DC1(1,j) ~= min(DC1(:,450:Xep)),DC1(1,j);
Xbp = j;
j=j+1;
end
I can understand the fact that DC1 is searching for the position of the minimum value term in itself by comparing it using the "min" function. What I do not understand is why there is that extra
",DC1(1,j)" bit. Do you know what it is? Thanks.

채택된 답변

Roger Wohlwend
Roger Wohlwend 2014년 5월 27일
The min function compares the vector DC1(:,450:Xep) to the scalar DC1(1,j). The result is a vector that is identical to DC1(:,450:Xep) except that all values that are greater than DC1(1,j) are replaced by DC1(1,j). Then this vector is compared to DC(1,j).
If Xep > 451 then DC1(:,450:Xep) is a vector and as I just wrote the result of the min function is a vector. When you compare a scalar to a vector you get a vector of logical values, but to control a loop you should not use a vector of logicals but only one single logical. If you provide a vector, I'm not completely sure, what Matlab does. I would avoid it because you risk that the code does not always do what it should. Perhaps the guy who wrote the code meant something like this:
while DC(1,j) ~= min(DC1(:,450:Xep))
That would make more sense.
  댓글 수: 1
Adam
Adam 2014년 5월 27일
well originally when I found the code it was simply on the next line like this:
while DC1(1,j) ~= min(DC1(:,450:Xep))
,DC1(1,j);
But because of that "," i figured he ment to leave it on the first line but took it down for keeping the code clean or prevent lines from getting to long.

댓글을 달려면 로그인하십시오.

추가 답변(2개)

lvn
lvn 2014년 5월 27일
I guess it is a remainder of some old code. It can be removed, it does nothing!

George Papazafeiropoulos
George Papazafeiropoulos 2014년 5월 27일
The extra
,DC1(1,j)
bit is the next command. You can erase the initial comma and press enter to get it in the next line.

Community Treasure Hunt

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

Start Hunting!

Translated by