Program for matrix multiplication

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 Comments

You have not defined for us the result you want for empty matrices. Also beware that a matrix is considered empty by MATLAB if any dimension of it is 0, so the matrices of size 5 x 0 or 0 x 17 or 0 x 0 would all be considered empty. If 5 x 0 is multiplied by 0 x 17 are you looking to return a 5 x 17 matrix or an empty matrix?
Your program fails, because Matlab uses a 1-based index. For i==0 or j==0 the expression "C(i,j)" causes an error.
I just checked..
P = zeros(5,0)
P = 5×0 empty double matrix
Q = zeros(0,17)
Q = 0×17 empty double matrix
P*Q
ans = 5×17
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I would have expected empty, not all zeros.
Paul
Paul on 18 Feb 2021
Edited: Paul on 18 Feb 2021
I would have too. But as it turns out this result is documented behavior
doc mtimes
Also
>> ones(3,0)*ones(0,2)
ans =
0 0
0 0
0 0
>> nan(3,0)*nan(0,2)
ans =
0 0
0 0
0 0
If you try to multiply two (non-scalar) matrices, the number of columns in the first must match the number of rows in the second. If the first matrix A is of size [r, k] and the second matrix B is of size [k, c] the result C = A*B must have size exactly [r, c]. If k is 0 then you could multiply two empty matrices (empty arrays in MATLAB must have one of the elements of their size vector equal to 0) and receive a non-empty result.
This is the mathematically correct behavior. We can argue about what the elements in that non-empty result should be but IMO the two most obvious candidates would be 0 and NaN. If you were to implement matrix multiplication naively you might start off by initializing the result to 0 then iterating over r, k, and c and adding the product of the appropriate elements of A and B to the correct place in C. For empties, this would simplify to initializing the result to 0 then performing no loop iterations.
Paul
Paul on 20 Feb 2021
Edited: Paul on 20 Feb 2021
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.

Sign in to comment.

Answers (4)

Azzi Abdelmalek
Azzi Abdelmalek on 12 Oct 2012
Edited: Azzi Abdelmalek on 12 Oct 2012
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
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
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 Comments

%5 x 1 * 1 x 3 would expect 5 x 3 output
my_matrix_mult(ones(5,1), ones(1,3))
m = 5
n = 1
k = 1
l = 3
ans = 5×5
1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0
But your code generates 5 x 5 output.
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
John D'Errico
John D'Errico on 17 Feb 2021
Edited: John D'Errico on 17 Feb 2021
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.
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

Sign in to comment.

Sweetie
Sweetie on 8 Nov 2023

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 Comment

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.

Sign in to comment.

Categories

Products

Asked:

on 12 Oct 2012

Commented:

on 8 Nov 2023

Community Treasure Hunt

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

Start Hunting!