Program for matrix multiplication
45 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Paul
il 20 Feb 2021
Modificato: Paul
il 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.
Risposte (4)
Azzi Abdelmalek
il 12 Ott 2012
Modificato: Azzi Abdelmalek
il 12 Ott 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
0 Commenti
Stun Dominic
il 2 Dic 2020
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
0 Commenti
Simon Profuß
il 15 Feb 2021
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 Commenti
John D'Errico
il 17 Feb 2021
Modificato: John D'Errico
il 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.
Simon Profuß
il 20 Feb 2021
Modificato: Simon Profuß
il 20 Feb 2021
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
il 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 Commento
John D'Errico
il 8 Nov 2023
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.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!