What's wrong in this code? why is this not displaying whole matrix?and how to sum whole matrix?

1 回表示 (過去 30 日間)
M=input('Enter number of manufacturers'); D=input('Enter number of distributers'); O=1; T=1; R=1; Z=1; for m=1:M; for d=1:D; Cmd=zeros(M:D); Cmd(m,d)=input('Enter traveling cost for manufacturer to distributer'); disp(Cmd); end end

採用された回答

Rahul Kalampattel
Rahul Kalampattel 2017 年 2 月 22 日
編集済み: Rahul Kalampattel 2017 年 2 月 22 日
You're overwriting the matrix Cmd every iteration because of the line Cmd=zeros(M:D). Take it outside of both for loops.
M=input('Enter number of manufacturers: ');
D=input('Enter number of distributers: ');
O=1; T=1; R=1; Z=1;
Cmd=zeros(M:D);
for m=1:M
for d=1:D
Cmd(m,d)=input('Enter traveling cost for manufacturer to distributer: ');
disp(Cmd);
end
end
To sum over all the elements of the matrix you can use sum(Cmd(:)).
  1 件のコメント
Aimen Mujahid
Aimen Mujahid 2017 年 2 月 22 日
O=1 for u=1:U; Cuo=zeros(u:O); Cuo(u,O)=input('Enter traveling cost for retailer to collection center'); end SumCuo=Sum(Cuo(:)); disp(SumCuo);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by