Program for matrix multiplication

45 次查看(过去 30 天)
sss dzu
sss dzu 2012-10-12
评论: Stephen23 2023-11-8
I wrote program to perform matrix product c=a*b
Program is good , but when I try run it by empty matrix, it was stuck
Can anyone help me to edit my program to run for all type of matrix, included [] matrix
This is I got so far
function [ C ] = my_matrix_mult( A,B )
[m,n]=size(A);
[k,l]=size(B);
if(n~=k)
C=[];
disp('Error, not able to multiply matrices');
return
end
C=zeros(m,1);
for i=0:m;
for j=0:l;
for p=0:n;
flag=1;
C(i,j)=C(i,j)+ A(i,p)*B(p,j);
end
end
end
end
  7 个评论
Paul
Paul 2021-2-20
编辑:Paul 2021-2-20
Is there a formal, mathematical definition of an empty matrix and operations thereon? I'd like to read about that if there is a reference. I recall that, long ago, Matlab documenation stated that the empty matrix implementation was a TMW invention.
I realize that there is some appeal to forcing the result of [r 0] * [0 c] = [r c], which is not an empty matrix. Is there a use case where one would intentionally take advantage of that behavior? It seems more likely to silently cause unexpected results.
Given the result is non-empty, it seems more natural that it fill with NaN. Why would multipying two objects that don't contain numbers produce a result that does? And NaN are more likely to be noticed in the end result.
From doc mtimes: "For example, if A is an m-by-0 empty matrix and B is a 0-by-n empty matrix, then A*B is an m-by-n matrix of zeros." I suggest deleting the "For example." As written it sounds like there might be other corner cases out there. Deleting that clause makes the rest a simple statement of fact.

请先登录,再进行评论。

回答(4 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-10-12
编辑:Azzi Abdelmalek 2012-10-12
In your first if use
if(n~=k) | m==0 | k==0
C=[];
disp('Error, not able to multiply matrices');
return
end
when A=B=[] , A and B have the same size . the condiition n~=k is false

Stun Dominic
Stun Dominic 2020-12-2
function [ C ] = my_matrix_mult( A,B )
[m,n]=size(A);
[k,l]=size(B);
if(n~=k)
C=[];
disp('Error, not able to multiply matrices');
return
end
C=zeros(m,1);
for i=0:m;
for j=0:l;
for p=0:n;
flag=1;
C(i,j)=C(i,j)+ A(i,p)*B(p,j);
end
end
end
end

Simon Profuß
Simon Profuß 2021-2-15
This worked for me. I simply fixed the issue that the index of the matrix C must start at 1 for Matlab:
function [ C ] = my_matrix_mult( A,B )
[m n]=size(A)
[k l]=size(B)
if(n~=k)
C=[];
disp('Error, not able to multiply matrices');
return
end
C=zeros(m);
for i=1:m;
for j=1:l;
for p=1:n;
C(i,j)=C(i,j)+ A(i,p)*B(p,j);
end
end
end
end
  3 个评论
John D'Errico
John D'Errico 2021-2-17
编辑:John D'Errico 2021-2-17
Yes. the simple use of a 1-based index is a highly important factor here. At the same time, you have improperly preallocated the size of C, when m and l are not the same. As well, your code probably needs to cater to the case of empty array input, since then the result must also be empty. So this is in the correct direction but does not get all the way there.
Simon Profuß
Simon Profuß 2021-2-20
编辑:Simon Profuß 2021-2-20
I think this should this should take care of the issues you mentioned:
function [C] = my_matrix_mult(A,B)
[m n]=size(A)
[k l]=size(B)
if(n~=k)
C=[];
disp('Error, not able to multiply matrices');
return;
elseif isempty(A) || isempty(B)
C=[];
return;
end
C=zeros(m,l);
for i=1:m;
for j=1:l;
for p=1:n;
C(i,j)=C(i,j)+ A(i,p)*B(p,j);
end
end
end
end

请先登录,再进行评论。


Sweetie
Sweetie 2023-11-8

clc A=input['enter first matrix '] B=input['input second matrix'] [m,n]=size(A); [P,q]=size(B); if n~=P disp ('matrix multiplication is not possible') else C=A×B; end

  1 个评论
John D'Errico
John D'Errico 2023-11-8
Do you understand that what you wrote is not actually valid MATLAB syntax? Perhaps you can find the (multiple) syntax errors if you look at what you wrote.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by