我需要帮助简化这个嵌套parfor和for循环,其中vars,mats和数组是: Y是5000 x 147矩阵, numProperties是5000, PaymentTable是54000 x 4矩阵, boolFlag是5000个数组, LookUpID是5000个数组, numPeriods是5000个数组, PaymentType是5000个单元阵列
Y = cell2mat(Y);
Y = [zeros(numProperties,1) Y(:,5:end) zeros(numProperties,4)];
for idx=1:numProperties;
maxPeriod=numPeriods(idx)+1;
parfor jdx=2:maxPeriod;
for kdx=1:size(PaymentTable);
if boolFlag(idx)==1 && LookUpID(idx)==PaymentTable(kdx,2) && PaymentTable(kdx,4)~=0 && PaymentTable(kdx,3)==jdx-1 && Y(idx,jdx)==0;
if PaymentTable(kdx,1)==2 && strcmp(PaymentType(idx),'Slow');
NonQuickRepayments(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==2 && strcmp(PaymentType(idx),'Quick');
QuickRepayments(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==3;
MoneyFlows(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==4;
Outstanding(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==5;
IntrinsicValue(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==6;
OtherValue(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==7;
ForexAdjustment(idx,jdx)=PaymentTable(kdx,4);
elseif PaymentTable(kdx,1)==8;
BalanceToDispose(idx,jdx)=-PaymentTable(kdx,4)+BalanceToDispose(idx,jdx);
elseif PaymentTable(kdx,1)==9;
BalanceToDispose(idx,jdx)=-PaymentTable(kdx,4)+BalanceToDispose(idx,jdx);
elseif PaymentTable(kdx,1)==10;
BalanceToDispose(idx,jdx)=-PaymentTable(kdx,4)+BalanceToDispose(idx,jdx);
elseif PaymentTable(kdx,1)==11;
BalanceToDispose(idx,jdx)=-PaymentTable(kdx,4)+BalanceToDispose(idx,jdx);
end;
end;
end;
end;
end;
答案 0 :(得分:1)
我没有看到简化for
循环的方法。代码看起来不像是矢量化的,如果要枚举三个变量,则需要三个循环。
您可以简化长if/elseif
语句;请改用switch
。此外,案例8至11做同样的事情。
switch PaymentTable(kdx,1)
case 2
if strcmp(PaymentType(idx),'Slow');
NonQuickRepayments(idx,jdx)=PaymentTable(kdx,4);
elseif strcmp(PaymentType(idx),'Quick');
QuickRepayments(idx,jdx)=PaymentTable(kdx,4);
end
case 3
MoneyFlows(idx,jdx)=PaymentTable(kdx,4);
case 4
Outstanding(idx,jdx)=PaymentTable(kdx,4);
case 5
IntrinsicValue(idx,jdx)=PaymentTable(kdx,4);
case 6
OtherValue(idx,jdx)=PaymentTable(kdx,4);
case 7
ForexAdjustment(idx,jdx)=PaymentTable(kdx,4);
case {8, 9, 10, 11}
BalanceToDispose(idx,jdx)=-PaymentTable(kdx,4)+BalanceToDispose(idx,jdx);
end
end