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
Walter Roberson
2012년 10월 12일
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?
Jan
2012년 10월 12일
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)
Q = zeros(0,17)
P*Q
I would have expected empty, not all zeros.
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
Steven Lord
2021년 2월 20일
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.
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
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
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ß
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
%5 x 1 * 1 x 3 would expect 5 x 3 output
my_matrix_mult(ones(5,1), ones(1,3))
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
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ß
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
2023년 11월 8일
0 개 추천
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
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.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!