Filter löschen
Filter löschen

Program for matrix multiplication

45 Ansichten (letzte 30 Tage)
sss dzu
sss dzu am 12 Okt. 2012
Kommentiert: Stephen23 am 8 Nov. 2023
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 Kommentare
Paul
Paul am 20 Feb. 2021
Bearbeitet: Paul am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Azzi Abdelmalek
Azzi Abdelmalek am 12 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 12 Okt. 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

Stun Dominic
Stun Dominic am 2 Dez. 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

Simon Profuß
Simon Profuß am 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 Kommentare
John D'Errico
John D'Errico am 17 Feb. 2021
Bearbeitet: John D'Errico am 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ß
Simon Profuß am 20 Feb. 2021
Bearbeitet: Simon Profuß am 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

Melden Sie sich an, um zu kommentieren.


Sweetie
Sweetie am 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 Kommentar
John D'Errico
John D'Errico am 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.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by