Borrar filtros
Borrar filtros

Program for matrix multiplication

45 visualizaciones (últimos 30 días)
sss dzu
sss dzu el 12 de Oct. de 2012
Comentada: Stephen23 el 8 de Nov. de 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 comentarios
Paul
Paul el 20 de Feb. de 2021
Editada: Paul el 20 de Feb. de 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.

Iniciar sesión para comentar.

Respuestas (4)

Azzi Abdelmalek
Azzi Abdelmalek el 12 de Oct. de 2012
Editada: Azzi Abdelmalek el 12 de Oct. de 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 el 2 de Dic. de 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ß el 15 de Feb. de 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 comentarios
John D'Errico
John D'Errico el 17 de Feb. de 2021
Editada: John D'Errico el 17 de Feb. de 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ß el 20 de Feb. de 2021
Editada: Simon Profuß el 20 de Feb. de 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

Iniciar sesión para comentar.


Sweetie
Sweetie el 8 de Nov. de 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 comentario
John D'Errico
John D'Errico el 8 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by