### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## While loop does not repeat

Shuxin Yang

### Shuxin Yang (view profile)

さんによって質問されました 2019 年 5 月 16 日

### Shuxin Yang (view profile)

さんによって コメントされました 2019 年 6 月 17 日 3:44
Jan

### Jan (view profile)

さんの 回答が採用されました
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);
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];
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;
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;
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

#### 0 件のコメント

サインイン to comment.

## 1 件の回答

2019 年 5 月 16 日

### Jan (view profile)

2019 年 5 月 16 日
採用された回答

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!

Shuxin Yang

### Shuxin Yang (view profile)

2019 年 6 月 17 日 3:44