当前位置: 首页 > 工具软件 > Dimensions > 使用案例 >

matlab must agree,运行错误Matrix dimensions must agree

邹阳
2023-12-01

matlab运行上述程序时,出现如下错误,如何解决呢?不知道如何更正

??? Error using ==> minus

Matrix dimensions must agree.

矩阵的维度在数学运算符减号的两边不区配:zeros(1,m)的维度为1x3, Y(:,i)'的维度为1x2

f=[zeros(1,m)-Y(:,i)']; 如果是减号运算,[]是不需要,如果需要[],意味着矩阵的组合;

Aeq=[X(:,k)' zeros(1,s)

Eii*X(:,i)'-Y(:,i)']; 同理。

所以楼主需要将减号去掉。

那么这个线性规划程序就能跑起来。

-------------------------------------------------------------------------

clear

X=[30  60  55  40  70

25  40  70  30  90

130 150 120 70  180

];

Y=[35 43 76 52 63

60 80 53 42 71

];

n=size(X',1);m=size(X,1);s=size(Y,1);E=zeros(n,n);w=zeros(n,n);

A=[-X' Y']; b=zeros(n,1);

LB=zeros(m+s,1);UB=[];

for i=1:n

Aeq=[X(:,i)' zeros(1,s)]; beq=1;

f=[zeros(1,m) Y(:,i)'];

w(:,i)=linprog(f,A,b,Aeq,beq,LB,UB);

Eii=Y(:,i)'*w(m+1:m+s,i);

for k=1:n

f=[zeros(1,m) Y(:,k)'];

Aeq=[X(:,k)' zeros(1,s)

Eii*X(:,i)' Y(:,i)'];

beq=[1

0];

v=linprog(f,A,b,Aeq,beq,LB,UB);

E(i,k)=(Y(:,k)'*v(m+1:m+s))/(X(:,k)'*v(1:m));

end

end

mean(E)

[Y,I]=sort(mean(E));

fliplr(I)

%

-------------------------------------------------------------------------

运行结果如下:

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

Optimization terminated.

>> E

E =

1.0e-09 *

0.1030    0.0695    0.0817    0.0001    0.0000

0.1532    0.0049    0.0554    0.0000    0.1323

0.1575    0.0024    0.0009    0.1030    0.0029

0.0000    0.0234    0.0814    0.0347    0.0000

0.2348    0.0527    0.0236    0.0010    0.0000

>> w

w =

0.0006    0.0070    0.0022    0.0001    0.0058

0.0291    0.0141    0.0018    0.0052    0.0014

0.0020    0.0001    0.0062    0.0120    0.0026

0.0000    0.0000    0.0000    0.0000    0.0000

0.0000    0.0000    0.0000    0.0000    0.0000

 类似资料: