Tuesday, June 16, 2009

do till condition is met "continue" example

This is a nice example.



clear

y = 1:100;
count = 0
for j = 1:length(y)

if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end



This takes x until y(j)>50. After the condition is met it simply passes out.


Better explained by:
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end

continue keeps on taking values does not break the chain.
while break breaks it and comes out of the loop



Contrasted over the
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end


lastly;


clear
clc
y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)<20||y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end

this does not even work while continue will keep looking for the situation.
Break just breaks while continue keep working escaping to the next one and checking!

Thursday, June 11, 2009

saving .mat file as per the iteration

I wanted to save the mat files as the iteration progressed.
here is the quick solution:
it saves the mat file with number of iteration as the file name: viz: 100.mat, 200.mat etc.
This is handy when you want to compare the results of the intermediate results as you progress the iteration.





if rem(nn,SS/10)==0
disp(['iteration: ', num2str(nn), ' of ', num2str(SS)]);
save ([num2str(nn)])
end