While loop does not repeat

Sorry about this long and silly code. I really need to know why this loop does not work properly. It is killing me. The code works just fine with one firm at a time, however, the loop only run for the first element in the "first_firm".
For example, without the "while i<=size(first_firm,1)" loop, load each firm by hand, the code works find. By adding the outside loop, I'm expecting that all firms in the "first_firm" matrix can be ran automaticly. However, it only runs the first one.
Therefore, my question is why does the loop always stop after running the first firm?
first_firm=[2502;2914;3382;4063;4452;4502;4523;4901;5108;6367;6902;6954;6971;
7203;7267;7269;7270;7751;8035;8316;8766;8801;8830;9020;9021;9022;9432;9433;
9735;9983;9984];
indf = 1;
while i <= size(first_firm,1)
n_firm = first_firm(indf);
load(['/pathname/',num2str(n_firm),'_P1_first_firm_before.mat']);
P1(P1(:,8)==0,:)=[];
P1_am = P1(P1(:,2)<=113001 & P1(:,2)>=85900,:);
P1_pm = P1(P1(:,2)<=150000 & P1(:,2)>=122900,:);
if P1_am(1,[3,4])~=0
P1_am(1,:)=P1_am(2,:);
end
if P1_pm(1,[3,4])~=0
P1_pm(1,:)=P1_pm(2,:);
end
P2=[P1_am;P1_pm];
i=1;
while i < size(P2,1)
if P2(i,3)==0 && P2(i,4)==0
I(i,:)=P2(i,:);
elseif P2(i,3)~=0 && P2(i,4)~=0 && P2(i,8)==1
I(i,:)=P2(i-1,:);
I(i,6)=P2(i-1,6)-P2(i,4);
elseif P2(i,3)~=0 && P2(i,4)~=0 && P2(i,8)==-1
I(i,:)=P2(i-1,:);
I(i,22)=P2(i-1,22)-P2(i,4);
end
i=i+1;
end
i=1;
while i < size(I,1)
if I(i,3)~=0 && I(i,4)~=0
I(i,:)=I(i-1,:);
end
if I(i,6)==0
I(i,[5:18])=I(i,[7:20]);
I(i,[19:20])=I(i+1,[19:20]);
elseif I(i,22)==0
I(i,[21:34])=I(i,[23:36]);
I(i,[34:36])=I(i+1,[34:36]);
end
i=i+1;
end
I_am = I(I(:,2)<=113001 & I(:,2)>=85900,:);
I_pm = I(I(:,2)<=150000 & I(:,2)>=122900,:);
Price = [5:2:35];Volume = [6:2:36];
ask_P = [5:2:19];bid_P = [21:2:35];
ask_V = [6:2:20];bid_V = [22:2:36];
mkt_odr_p_am = find(P1_am(:,3)~=0)-1;
mkt_ord_p_pm = find(P1_pm(:,3)~=0)-1;
i = 1;
while i < size(I_am,1)
if I_am(i, Price) == I_am(i+1, Price)
I1(i, [1:4,Price]) = I_am(i, [1:4,Price]);
I1(i, Volume) = I_am(i+1, Volume) - I_am(i, Volume);
else
I1(i, [1:4,Price]) = I_am(i+1, [1:4,Price]);
I1(i, Volume) = 0;
if sum(I_am(i, ask_P) > I_am(i+1, ask_P))>0
I3 = find(I_am(i, ask_P) ~= I_am(i+1, ask_P));
I1(i, ask_P(I3(1))) = I_am(i+1, ask_P(I3(1)));
I1(i, ask_V(I3(1))) = I_am(i+1, ask_V(I3(1)));
elseif sum(I_am(i, ask_P) < I_am(i+1, ask_P))>0
I3 = find(I_am(i, ask_P) ~= I_am(i+1, ask_P));
I1(i, ask_P(I3(1))) = I_am(i, ask_P(I3(1)));
I1(i, ask_V(I3(1))) = -I_am(i, ask_V(I3(1)));
elseif sum(I_am(i, bid_P) > I_am(i+1, bid_P))>0
I3 = find(I_am(i, bid_P) ~= I_am(i+1, bid_P));
I1(i, bid_P(I3(1))) = I_am(i, bid_P(I3(1)));
I1(i, bid_V(I3(1))) = -I_am(i, bid_V(I3(1)));
elseif sum(I_am(i, bid_P) < I_am(i+1, bid_P))>0
I3 = find(I_am(i, bid_P) ~= I_am(i+1, bid_P));
I1(i, bid_P(I3(1))) = I_am(i+1, bid_P(I3(1)));
I1(i, bid_V(I3(1))) = I_am(i+1, bid_V(I3(1)));
end
end
i = i+1;
end
I1(mkt_odr_p_am,:)=[];
i = 1;
while i < size(I_pm,1)
if I_pm(i, Price) == I_pm(i+1, Price)
I2(i, [1:4,Price]) = I_pm(i, [1:4,Price]);
I2(i, Volume) = I_pm(i+1, Volume) - I_pm(i, Volume);
else
I2(i, [1:4,Price]) = I_pm(i+1, [1:4,Price]);
I2(i, Volume) = 0;
if sum(I_pm(i, ask_P) > I_pm(i+1, ask_P))>0
I3 = find(I_pm(i, ask_P) ~= I_pm(i+1, ask_P));
I2(i, ask_P(I3(1))) = I_pm(i+1, ask_P(I3(1)));
I2(i, ask_V(I3(1))) = I_pm(i+1, ask_V(I3(1)));
elseif sum(I_pm(i, ask_P) < I_pm(i+1, ask_P))>0
I3 = find(I_pm(i, ask_P) ~= I_pm(i+1, ask_P));
I2(i, ask_P(I3(1))) = I_pm(i, ask_P(I3(1)));
I2(i, ask_V(I3(1))) = -I_pm(i, ask_V(I3(1)));
elseif sum(I_pm(i, bid_P) > I_pm(i+1, bid_P))>0
I3 = find(I_pm(i, bid_P) ~= I_pm(i+1, bid_P));
I2(i, bid_P(I3(1))) = I_pm(i, bid_P(I3(1)));
I2(i, bid_V(I3(1))) = -I_pm(i, bid_V(I3(1)));
elseif sum(I_pm(i, bid_P) < I_pm(i+1, bid_P))>0
I3 = find(I_pm(i, bid_P) ~= I_pm(i+1, bid_P));
I2(i, bid_P(I3(1))) = I_pm(i+1, bid_P(I3(1)));
I2(i, bid_V(I3(1))) = I_pm(i+1, bid_V(I3(1)));
end
end
i = i+1;
end
I2(mkt_ord_p_pm,:)=[];
I=[I1;I2];
save(['/pathname/',num2str(n_firm),'_P2_first_firm_before.mat'],'P2')
save(['/pathname/',num2str(n_firm),'_I1_first_firm_before.mat'],'I')
indf = indf+1
end

 Risposta accettata

Jan
Jan il 16 Mag 2019
Modificato: Jan il 16 Mag 2019
The code should not work at all, because i is not defined before
while i <= size(first_firm,1)
But then Matlab assumes you mean the imaginary unit.
After
indf = 1;
and the trailing
indf = indf+1
I guess you meant:
while indf <= size(first_firm,1)
A for loop would be nicer:
for indf = 1:numel(first_firm)
...
end
By the way, a:b is a vector already and additional square brackets can slow down the execution:
% I(i,[5:18])=I(i,[7:20]);
I(i, 5:18) = I(i, 7:20); % Faster!

1 Commento

Shuxin Yang
Shuxin Yang il 17 Giu 2019
Thank you very much for your reply.
In fact, while i <= size(first_firm,1) was a typo of this question and it is indf in the original code.
Somehow the code works without changes. I have no idea why it did not work at the first time.
I tried your suggestion of for loop and it is indeed better. Thank you very much.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by