How do I compare the result of the output of a loop with the previous output of the loop?
    5 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Janna Hinchliff
 le 7 Déc 2018
  
    
    
    
    
    Réponse apportée : Stephen23
      
      
 le 7 Déc 2018
            If I output data from a loop into a cell array such that each iteration of the is stored as an individual cell in a single cell array, how can I write a code where I compare the the ith term in the loop to the (i-1)th term? I want to compare the number of rows of each cell, e.g. 
for i = 1:5
    matrix = ones(i,2); % create some matrix - in my real code this would not be in ascending order, and some matrices would have the same number of rows
    matrixcell{i} = matrix; % create some cell array
    % Here I need to compare the number of rows
    if size(matrixcell{i})<size(matrixcell{i-1})
        disp('current result has less rows')
    elseif size(matrixcell{i})==size(matrixcell{i-1})
        disp('current result has same number of rows')
    else
        disp('current result has more rows')
    end
end
I understand that the reason this doesn't work is that matrixcell{i-1} doesn't exist for i=1, but this is the idea of the comparison I need. I don't need to compare the i=1 case to anything - this can just be printed or ignored. Does this require a while loop?
0 commentaires
Réponse acceptée
  Stephen23
      
      
 le 7 Déc 2018
        Put your entire if... ifelse ... end inside another if:
if i>1
    if size(...)
        ...
    elseif size(...)
        ...
    end
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

