2 回答

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
將下列語(yǔ)句保存在一個(gè)文件名為matrix.m的Matlab腳本文件里:
clc;clear;
disp('This program multiplies two m x m matrices A and B.');
m=input('Enter a value for m: ');
A=zeros(m,m);B=A;C=A;
for i=1:m
str=sprintf('Enter row %d of A as an array: ',i);
A(i,:)=input(str);
end
for i=1:m
str=sprintf('Enter row %d of B as an array: ',i);
B(i,:)=input(str);
end
for i=1:m
for j=1:m
for k=1:m
C(i,j)=C(i,j)+A(i,k)*B(k,j);
end
end
end
str='AxB=';
dlmwrite('matrix.txt',str,'delimiter',' ','newline','pc','precision','%s');
dlmwrite('matrix.txt',C,'-append','delimiter',' ','newline','pc','precision','%.1f','roffset',1);

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
Cij = sum(Aik*Bkj) (k=1:m)
按上式寫個(gè)嵌套循環(huán)就搞定了,復(fù)雜度是n^2
添加回答
舉報(bào)