How to keep track of last change in a variable inside the loop?
    3 ビュー (過去 30 日間)
  
       古いコメントを表示
    
Suppose I have a variable NN, and I am applying for loop 40 times....each loop may or may not change the variable NN....I want to know what is the last loop where the variable NN changes....for example:if the last change of NN occurs at 35th loop and no changes thereafter then, how should I know that it last changes at 35th loop by using the code?... can anyone help
0 件のコメント
採用された回答
  Image Analyst
      
      
 2015 年 7 月 25 日
        You can keep track of NN and then compare it to the prior one and log when it changes.
lastNN = -999999; % Something you know it will never be.
indexOfLastChange  = 1;
for k = 1 : 40
    NN = GetNewNN(k);  % Somehow get a new NN, or maybe not!
    % Now compare this NN to the prior one.
    if NN ~= lastNN
        % It changed!  Log it
        indexOfLastChange = k;
    end
    % Save the current NN as the last one:
    lastNN = NN;
end
% Notify user of the time it last changed.
message = sprintf('NN last changed during iteration %d.', indexOfLastChange)
uiwait(helpdlg(message));
その他の回答 (1 件)
  Azzi Abdelmalek
      
      
 2015 年 7 月 25 日
        
      編集済み: Azzi Abdelmalek
      
      
 2015 年 7 月 25 日
  
      You can use a counter. Example
for k=1:40
   a=sin(k)
   if a>0.5
      y=a
      ii=k
   end
end
disp(ii)
参考
カテゴリ
				Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
			
	製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


