Same code I can successfully run in R2021b but in R2016a it is showing Error using - Matrix dimensions must agree. What should I change to run this in R2016a?

1 回表示 (過去 30 日間)
function [facilities,R,Dij,E,C]=a(r1,r2,d1,d2,i,j)
tic
facilities=(1:i)';
R = randi([r1 r2],[i 2]);
R(:,2)=(R(:,1))+(R(:,2));
Dij=randi([d1 d2],[i,j]);
E=zeros(i,1);
E(:)=10^-6;
for m=1:i
for n=1:j
C = max((R(:,2)-Dij(:,:)),0)./max(max((R(:,2)-R(:,1)),(R(:,2)-Dij(:,:))),E(:,1));
n=n+1;
end
m=m+1;
end
toc
end
% Same code I can successfully run in R2021b but in R2016a it is showing Error using - Matrix dimensions must agree. What should I change to run this in R2016a?
  3 件のコメント
Sourasis Chattopadhyay
Sourasis Chattopadhyay 2022 年 12 月 8 日
  • In line number 12 .
  • [facilities,R,Dij,E,C]=a(10,20,30,40,5,3)
Stephen23
Stephen23 2022 年 12 月 8 日
編集済み: Stephen23 2022 年 12 月 8 日
You can read more about implicit array expansion here:
The code throws an error on R2016a because it does not have implicit expansion.

サインインしてコメントする。

採用された回答

DGM
DGM 2022 年 12 月 8 日
編集済み: DGM 2022 年 12 月 8 日
R2016b introduced implicit array expansion. You're relying on that. The loops are also unnecessary.
i = 10;
j = 5;
r1 = 1;
r2 = 10;
d1 = 1;
d2 = 10;
facilities=(1:i)';
R = randi([r1 r2],[i 2]);
R(:,2)=(R(:,1))+(R(:,2));
Dij=randi([d1 d2],[i,j]);
E=zeros(i,1);
E(:)=10^-6;
num = max(bsxfun(@minus,R(:,2),Dij),0);
den = max((R(:,2)-R(:,1)),E(:,1)); % these terms agree
den = bsxfun(@max,bsxfun(@minus,R(:,2),Dij),den);
C = num./den;
I tested this in R2015b

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by