How to repeat a for loop
2 views (last 30 days)
Show older comments
Hello All,
I have the following code and I need some help to get the commands to repeat the loops whenever the matrix start_pf is not equal to the matrix end_final by using for the repetition the matrix end_final as the starting matrix.
num_cols=length(data)
start_pf=[data{1,1}(1,1),data{1,2}(1,1),data{1,3}(1,1),data{1,4}(1,1),data{1,5}(1,1),data{1,6}(1,1)];
for i=1:1:num_cols
if i==1
iteration=[reshape(data{i},length(data{i}),1),repmat(start_pf(1,i+1:end),length(data{i}),1)];
else
iteration=[repmat(iteration(best1,1:i-1),length(data{i}),1),reshape(data{i},length(data{i}),1),repmat(iteration(best1,i+1:end),length(data{i}),1)];
end
for j=1:1:length(data{i})
y(j)=a(1,iteration(j,1)) + a(2,iteration(j,2)) + a(3,iteration(j,3)) + a(4,iteration(j,4)) + a(5,iteration(j,5)) + a(6,iteration(j,6)) ;
x(j)=function1(iteration(j,1),iteration(j,2),iteration(j,3),iteration(j,4),iteration(j,5),iteration(j,6));
z(j)=y(j) + y(j);
best1=find(z==min(z));
end
end_pf=iteration(best1,:);
end
As I mentioned i need some commands to help comparing the matrices start_pf and end_pf and if they are not equal I want to repeat the loops by using this time the end_pf as the starting point . I try the following but it did not work:
if (first_row_opt~=init_portfolio)
init_portfolio=first_row_opt;
end
Thanks
0 Comments
Answers (1)
James Wright
on 26 May 2012
Hi, Have you tried using a while loop instead?
i.e.
while first_row_opt ~= init_portfolio
all your code
end
of course you will have to make a few other changes to your code to re-assign variable you want to change each loop. hope this helps,
James
0 Comments
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!