“下标分配尺寸不匹配。”想不通

时间:2015-08-18 06:46:13

标签: matlab loops if-statement for-loop matrix

我正在尝试运行以下循环(现在有点草率/慢):

Sa1 = NaN(50,100);
Sd1 = NaN(50,100);
S1 = NaN(50,100);
e1 = NaN(50,100);
UdSd1 = NaN(50,100);
UdSa1 = NaN(50,100);
UaSd1 = NaN(50,100);
UaSa1 = NaN(50,100);
Ta1 = NaN(50,100);
Td1 = NaN(50,100);
p1 = NaN(50,100);
q1 = (1);
r = (2/3);
paths = 100;
p1 = [linspace(0.01,1); zeros(50, 100)];
%%
for t=1:5
     for i=1:paths
         if (p1(t,i) < .5)
             e1(t,i) = (1-r*(1-p1(t,i)))*(1-q1*r);
             syms A D
             [A, D] = solve([A == (p1(t,i)*r*(e1(t,i)+q1*r)+(1-e1(t,i))*(1-p1(t,i))*(1+q1*r)*D+(1+q1*r)*(1-r)*p1(t,i)*e1(t,i))/((p1(t,i)+(1-e1(t,i))*(1-p1(t,i)))*(1+q1*r)), D == ((1-p1(t,i))*r*(1-e1(t,i)+q1*r)+e1(t,i)*p1(t,i)*(1+q1*r)*A)/(((1-p1(t,i))+(e1(t,i)*p1(t,i)))*(1+q1*r))], A<=1, A>=0, D<=1, D>= 0, [A, D], 'IgnoreAnalyticConstraints', true);
             solA1 = A;
             solD1 = D;
             vpa(solA1);
             vpa(solD1);
             Sa1(t,i) = vpa(solA1);
             Sd1(t,i) = vpa(solD1);
             S1(t,i) = p1(t,i)*Sa1(t,i) + (1-p1(t,i))*Sd1(t,i);
             UdSd1(t,i) = -(((1-p1(t,i))*(r*(Sd1(t,i)-((1-e1(t,i)+q1*r)/(1+q1*r)))^2)+(1-r)*(Sd1(t,i)^2))+e1(t,i)*p1(t,i)*(Sd1(t,i)-Sa1(t,i))^2);
             UdSa1(t,i) = -(((1-p1(t,i))*(r*(Sa1(t,i)-((1-e1(t,i)+q1*r)/(1+q1*r)))^2)+(1-r)*(Sa1(t,i)^2)));
             UaSd1(t,i) = -((p1(t,i)*(r*(Sd1(t,i)-((e1(t,i)+q1*r)/(1+q1*r)))^2)+(1-r)*(Sd1(t,i)-e1(t,i))^2));
             UaSa1(t,i) = -((p1(t,i)*(r*(Sa1(t,i)-((e1(t,i)+q1*r)/(1+q1*r)))^2)+(1-r)*(Sa1(t,i)-e1(t,i))^2) + (1-e1(t,i))*(1-p1(t,i))*(Sa1(t,i)-Sd1(t,i))^2);
             Ta1(t,i) = (1-p1(t,i))*(UaSa1(t,i)-UaSd1(t,i));
             Ta1(t,i) = max(0,Ta1(t,i));
             Td1(t,i) = p1(t,i)*(UdSd1(t,i)-UdSa1(t,i));
             Td1(t,i) = max(0,Td1(t,i));
             p1(t+1,i) = (Ta1(t,i) - Td1(t,i))*p1(t,i)*(1-p1(t,i)) + p1(t,i);
        else
            e1(t,i) = (1);
            syms A D
            [A, D] = solve([A == (p1(t,i)*r*(e1(t,i)+q1*r)+(1-e1(t,i))*(1-p1(t,i))*(1+q1*r)*D+(1+q1*r)*(1-r)*p1(t,i)*e1(t,i))/((p1(t,i)+(1-e1(t,i))*(1-p1(t,i)))*(1+q1*r)), D == (e1(t,i)*p1(t,i)*A)/((1-p1(t,i))+e1(t,i)*p1(t,i))], A<=1, A>=0, D<=1, D>= 0, [A, D], 'IgnoreAnalyticConstraints', true);
            solA1 = A;
            solD1 = D;
            vpa(solA1);
            vpa(solD1);
            Sa1(t,i) = vpa(solA1);
            Sd1(t,i) = vpa(solD1);
            S1(t,i) = p1(t,i)*Sa1(t,i) + (1-p1(t,i))*Sd1(t,i);
            UdSd1(t,i) = -(((1-p1(t,i))*(r*(Sd1(t,i)^2)))+e1(t,i)*p1(t,i)*(Sd1(t,i)-Sa1(t,i))^2);
            UdSa1(t,i) = -(((1-p1(t,i))*(r*(Sa1(t,i)^2))));
            UaSd1(t,i) = -((p1(t,i)*(r*(Sd1(t,i)-((e1(t,i)+q1*r)/(1+q1*r)))^2)+(1-r)*(Sd1(t,i)-e1(t,i))^2));
            UaSa1(t,i) = -((p1(t,i)*(r*(Sa1(t,i)-((e1(t,i)+q1*r)/(1+q1*r)))^2)+(1-r)*(Sa1(t,i)-e1(t,i))^2) + (1-e1(t,i))*(1-p1(t,i))*(Sa1(t,i)-Sd1(t,i))^2);
            Ta1(t,i) = (1-p1(t,i))*(UaSa1(t,i)-UaSd1(t,i));
            Ta1(t,i) = max(0,Ta1(t,i));
            Td1(t,i) = p1(t,i)*(UdSd1(t,i)-UdSa1(t,i));
            Td1(t,i) = max(0,Td1(t,i));
            p1(t+1,i) = (Ta1(t,i) - Td1(t,i))*p1(t,i)*(1-p1(t,i)) + p1(t,i);
         end
     end
 end

请原谅我,我是新手并且正在努力学习。 我的问题在于if/else p矩阵根据 t进程随每行发展。当p < .5时,它应该跟随第一部分"if,",否则第二部分"when."但是,一旦我点击t=3,代码就停止并说  "Subscripted assignment dimension mismatch."标记为i=51。我知道这是一个问题,因为代码正在发展,以前的<.5列现在是>.5,但我不能在我的生活中弄清楚如何解决它。

有人有什么想法吗?感谢帮助!

0 个答案:

没有答案