matlab 矩阵乘法

来源:百度知道 编辑:UC知道 时间:2024/06/21 16:28:34
原题如下,能做出来的,我再追加分数!

Part B (15 points) This program will multiply 2 m x m matrices and print the results to a file called ‘matrix.txt’
EXACTLY as shown in the example output at the bottom of this page (including the horizontal and vertical
lines). The user should enter a value for m and then input each of the matrix elements as shown in the example
output. Each matrix must be stored as a multidimensional (multiple-scripted) array. Each element in the output
should be printed to 1 decimal place. Assume all the matrix elements entered by the user are small enough that
each of the elements in the output matrix is less than 100.

Note that MatLab can perform matrix multiplication for you, i.e. if you have 2 m x m matrices named A and B,
typing A*B will multiply the 2 matrices and output the result. For this assignment, YOU CANNOT LET
MATLAB MULTIPLY THE MATRICES FOR YOU! You must write the algorithm which calculates eac

function matrix()
%%本函数注意以下几个方面
%%1.所有fprintf中\r\n也可以用\n表示 这个主要看你的文本查看器支持
%% 如果是WINDOWS的记事本看matrix.txt的话,最好用\r\n
%%2.输出语句fprintf(fid,'%.1f ',C(i,j));
%% 我觉得用fprintf(fid,'%4.1f ',C(i,j));产生的数据会更整齐一些
%% 不过你给的提示用fprintf(fid,'%.1f ',C(i,j));就可以了
disp('This program multiplies two m x m matrices A and B.')
m=input('Enter a value for m:');

%%input A
disp(' ');
A=zeros(m);
for i=1:m
info=sprintf('Enter row %d of A as an array:',i);
A(i,:)=input(info);
end

%%input B
disp(' ');
B=zeros(m);
for i=1:m
info=sprintf('Enter row %d of B as an array:',i);
B(i,:)=input(info);
end

%%C=A*B
C=zeros(m);
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

fid=f