Skip to content
Snippets Groups Projects
module4_looping.m 642 B
Newer Older
  • Learn to ignore specific revisions
  • vand's avatar
    vand committed
    %% module 4, looping
    
    %% example -- can't change iterator during for loop
    
    for i = 1:9
        disp(['i is ',num2str(i),', trying to change it to ',num2str(3*i)])
        i = 3*i; % don't do this (trying to change an iterator in a for loop)
    end
        
    
    %% example -- find largest number divisible with 7 and smaller than 100    
    
    i = 1;
    while i*7<100
        i = i+1;
    end
    disp(7*i) % this will be larger than 100 -- so condition from while does not hold
    
    %% example -- vectorization
    
    test = [3, 4, 8, 6, 5, 8, 3, 4, 7, 8, 8, 9, 4, 1, 3, 4, 6, 7, 9, 5, 3, 5, 7, 2, 5, 3];
    
    hist = zeros(1,9);
    for i = 1:numel(test)
        hist(test(i)) = hist(test(i))+1;
    end