problem with extra entries in a matrix!
    5 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Hello, I would like to fill a matrix but I have a problem in filling it, because I get some extra entries even though I have initialized my matrix to the required size. Please find the problematic loop bellow, and hope you can help, thank you.
 year = 1;
i = 0;
Nbrtot = 6;
MyResults  = zeros(Nbrtot, 1);
for l = 1:year
    for nbrrepetiton = 0:l
        for NoYes = [0 1]
            for semester = [1 2]
                i = i + 1;
                MyResults(i)  = i;
            end
        end
    end
end
Note: using this code My matrix MyResults has 2 extra entries.
4 Kommentare
Akzeptierte Antwort
  Stephen23
      
      
 am 22 Mai 2017
        
      Bearbeitet: Stephen23
      
      
 am 22 Mai 2017
  
      for l = 1:year          % one iteration
for nbrrepetiton = 0:l  % two iterations
for NoYes = [0 1]       % two iterations
for semester = [1 2]    % two iterations
Each nested loop multiplies the total iterations by its own number of iterations, so we get:
1x2x2x2 = 8
So we find that MATLAB is doing exactly what you told it to do: eight iterations in total. There is no reason to expect six iterations here.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Creating and Concatenating Matrices 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!


