How to find product of any matrix A

2 views (last 30 days)
I currently have the below as my function that I created to try and find the product of any matrix A. When I run the function, it keeps telling me that B is not inside any function. I don't know if I should change my function or what else I should do to fix the problem.
function prod=MatProd(A)
prod=1;
[m,n]=size(A);
for i=1:1:m
for j=1:1:n
prod=prod*A(i,j);
end
end
end
B=[12.2, 1.2, 2.4; 2,3,4; 2.5, 6.2,3.4];
MatProd(B)

Accepted Answer

the cyclist
the cyclist on 25 Jan 2021
Edited: the cyclist on 25 Jan 2021
Put the function definition
function prod=MatProd(A)
prod=1;
[m,n]=size(A);
for i=1:1:m
for j=1:1:n
prod=prod*A(i,j);
end
end
end
in a file named MatProd.m, and then call it using
B=[12.2, 1.2, 2.4; 2,3,4; 2.5, 6.2,3.4];
MatProd(B)
  2 Comments
Nicole Precourt
Nicole Precourt on 25 Jan 2021
When I input that code, if says MatProd "not enough input arguements. Error in MatProd (line 3) [m,n]=size(A);"
the cyclist
the cyclist on 25 Jan 2021
Are you sure you didn't call
MatProd()
instead of
MatProd(B)
?
That would give the error you are seeing.
If that's not the case, can you please be very specific about how you defined the function, and how you called it (e.g. from the command window, or from another function)?

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!