zhou7069 发表于 2008-12-12 08:31

矩阵维数对不上...

Initial_stock=5;
Weeks=40;
Sum=zeros(7,8);
Stock=0;
for Reorder_point=0:6
    for Order_quantities =1:8
      f=zeros(1,Weeks);
      Stock=Initial_stock;
      for w=1:1:Weeks
      Stock=Stock+f(w)*Order_quantities;
re=600; % Offset for random seed
for i=1:10000
xxx=i+re;
rand('seed',xxx);% Set the function to xxx state
X=rand(1);% Generate a uniformly distributed random variable over (0,1)
if(X<=0.04)&(X>=0)% Generate the random variable following the given distribution
D(i)=0;
elseif (X>0.04)&(X<=0.12)
Demand (i)=1;
elseif (X>0.12)&(X<=0.4)
Demand (i)=2;
elseif (X>0.4)&(X<=0.8)
Demand (i)=3;
elseif (X>0.8)&(X<=0.96)
Demand (i)=4;
elseif (X>0.96)&(X<=0.98)
Demand (i)=5;
elseif (X>0.98)&(X<=1)
Demand (i)=6;
end
Stock=Stock-Demand;   % the item amount at the end of the week
if Stock<0   
Sum(Reorder_point+1,Order_quantities)= Sum(Reorder_point+1,Order_quantities) +20; % stockout cost
Stock=0;
end
if Stock<=Reorder_point
Sum(Reorder_point+1,Order_quantities) = Sum(Reorder_point+1,Order_quantities) +10+5*Stock; %order replacement cost
else if Stock> Reorder_point
   Sum(Reorder_point+1,Order_quantities)= Sum(Reorder_point+1,Order_quantities)+5*Stock
end
end
end
end
end
end
mina=0;                           %initial value of re-order point
minb=0;                           %initial value of order quantity
minsum=sum(1,1);                  %comparison
for i=0:6
for j=1:8
if sum(i,j)<minsum
   minsum = sum(i,j)
    mina=i-1
minb=j;
end
end
end

就是要求一个order quantity和re-order point 一运行就说??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> m2_2 at 31
Stock=Stock-Demand;   % the item amount at the end of the week
再运行??? Subscripted assignment dimension mismatch.
Error in ==> m2_2 at 39
   Sum(Reorder_point+1,Order_quantities)= Sum(Reorder_point+1,Order_quantities)+5*Stock

小弟菜鸟一只 麻烦高手指点下非常感谢~!

zhou7069 发表于 2008-12-12 09:12

通过对"常见的程序出错问题整理"的学习 受益非浅把Demand (i) 后面的()去掉就能运行了 要不然维数对不上...不过运行很慢数字也很大估计还有其他错误

我想请问如果把DEMAND那段换成 Demand_frequency=rand(1)*100;
            Demand_frequency=ceil(Demand_frequency);
            if Demand_frequency<=4,
                Demand_quantity=0
            elseif Demand_frequency>=5 & Demand_frequency<=12,
                Demand_quantity=1
            elseif Demand_frequency>=13&Demand_frequency<=40,
                Demand_quantity=2
            elseif Demand_frequency>=41&Demand_frequency<=80,
                Demand_quantity=3
            elseif Demand_frequency>=81&Demand_frequency<=96,
                Demand_quantity=4
            elseif Demand_frequency>=97&Demand_frequency<=98,
                Demand_quantity=5
            else Demand_frequency>=99,
                Demand_quantity=6
            end
就运行的很快   如果可以的话 麻烦指点下为什么?

[ 本帖最后由 zhou7069 于 2008-12-12 09:15 编辑 ]

sigma665 发表于 2008-12-12 15:53

循环层数太多,当然慢了

ChaChing 发表于 2008-12-12 21:34

不好意思, 个人认为写程序还是要养成好习惯!
尽可能有层次, 简易的设定使用一行, 参考参考!
楼主有多馀的end未发现!

ch_j1985 发表于 2008-12-12 23:53

回复 楼主 zhou7069 的帖子

if Stock<=Reorder_point
Sum(Reorder_point+1,Order_quantities) = Sum(Reorder_point+1,Order_quantities) +10+5*Stock; %order replacement cost
else if Stock> Reorder_point
   Sum(Reorder_point+1,Order_quantities)= Sum(Reorder_point+1,Order_quantities)+5*Stock
end
end
end
end
end
end
红色代码是不是有问题?

zhou7069 发表于 2008-12-13 00:41

原帖由 ch_j1985 于 2008-12-12 23:53 发表 http://www.chinavib.com/forum/images/common/back.gif
if Stock Reorder_point
   Sum(Reorder_point+1,Order_quantities)= Sum(Reorder_point+1,Order_quantities)+5*Stock
end
end
end
end
end
end
红色代码是不是有问题?

我现在换了个思路在作了...非常感谢你~!

zhou7069 发表于 2008-12-13 00:42

原帖由 ChaChing 于 2008-12-12 21:34 发表 http://www.chinavib.com/forum/images/common/back.gif
不好意思, 个人认为写程序还是要养成好习惯!
尽可能有层次, 简易的设定使用一行, 参考参考!
楼主有多馀的end未发现!

是的是我写的太乱了我现在重新换了个思路写了   谢谢
页: [1]
查看完整版本: 矩阵维数对不上...