Matlab-While loops -


given array have print numbers in array must positive , divisible 2, if condition not hold print "didn't find" once.

i have run code "didnt find" message entire length of array. how code says once?

here's code:

numbers = [9 3 7 3]; = 1;     while <= length(numbers)     if numbers(i)>0 && mod(numbers(i),2) == 0         disp(numbers(i))     else         disp('didint find')         = + 1;     end end 

numbers = [9 3 7 3]; found = false; %this boolean flag used see if have found number fitting rules. if find no number, found still false @ end of loop  = 1:length(numbers) %a loop far more suited problem original while      if numbers(i)>0 && mod(numbers(i),2) == 0         disp(numbers(i))        found = true;      end end if ~found     disp('didn''t find'); end 

but in matlab no loop, in fact preferable not use loop. try following code:

ind = numbers > 0 & mod(numbers,2) == 0; if ~any(ind)     disp('didn''t find'); else         disp(numbers(ind)'); end 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -