matlab 矩阵乘法原题如下,能做出来的,我再追加分数!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 pa

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/16 20:33:47
matlab 矩阵乘法原题如下,能做出来的,我再追加分数!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 pa

matlab 矩阵乘法原题如下,能做出来的,我再追加分数!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 pa
matlab 矩阵乘法
原题如下,能做出来的,我再追加分数!
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 each of
the matrix elements.

The name of this program must be matrix.m

Example output #1 for Part B:

>> matrix

This program multiplies two m x m matrices A and B.
Enter a value for m: 4

Enter row 1 of A as an array: [ 1.3 2.1 2.5 5.1 ]
Enter row 2 of A as an array: [ 2.4 3.2 4.1 0.8 ]
Enter row 3 of A as an array: [ 2.3 1.2 1.4 1.5 ]
Enter row 4 of A as an array: [ 3.3 0.7 0.6 1.3 ]

Enter row 1 of B as an array: [ 1.2 1.3 1.4 1.5 ]
Enter row 2 of B as an array: [ 2.8 2.3 2.2 1.5 ]
Enter row 3 of B as an array: [ 1.5 0.5 1.1 2.2 ]
Enter row 4 of B as an array: [ 1.8 1.4 0.9 0.5 ]

>>

The file matrix.txt will look like:

A x B =
_ _
| 20.4 14.9 13.8 13.2 |
| 19.4 13.7 15.6 17.8 |
| 10.9 8.6 8.8 9.1 |
|_ 9.2 8.0 8.0 8.0 _|


(continued next page)

Example output #2 for Part B:

>> matrix

This program multiplies two m x m matrices A and B.
Enter a value for m: 2

Enter row 1 of A as an array: [ 1 2 ]
Enter row 2 of A as an array: [ 2 3 ]

Enter row 1 of B as an array: [ 3 1 ]
Enter row 2 of B as an array: [ 2 4 ]

The file matrix.txt will look like:

A x B =
_ _
| 7.0 9.0 |
|_ 12.0 14.0 _|


Note about spacing: If I enter numbers that are too large, the spacing in my output gets all screwed
up. Assume that when we test the program, we won’t enter numbers larger than the ones in this
example.

Note about formatting: The output should be displayed with the horizontal and vertical bars as
shown. If you do not include the bars, the maximum score you can receive on this problem is 13/15.
我也知道有点难,大家努力做吧,如果实在做不出来,我也会看情况把分数给最努力的那个的,后面我还会再追加分数,大家看我提问过的历史记录,我说到做到的!

matlab 矩阵乘法原题如下,能做出来的,我再追加分数!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 pa
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=fopen('matrix.txt','w');
fprintf(fid,'A x B =\r\n');
fprintf(fid,'_ _ \r\n');
for i=1:m
if =m
fprintf(fid,'| ');
else
fprintf(fid,'|_ ');
end
for j=1:m
fprintf(fid,'%.1f ',C(i,j));
end
if =m
fprintf(fid,'|\r\n');
else
fprintf(fid,'_|\r\n');
end
end
fclose(fid);