How to input a matrix as input into a function

4 views (last 30 days)
I'm trying to input a matrix as my input into the function I'm making. When inputting the matrix in the command window when calling the script, I get the error unexpected matlab expression. I'm not sure of the syntax to input a matrix in or if it is even possible to in the first place.
Here is my code below if that helps at all:
function determinantOutput =myDeterminantCalc(a)
if size(a)==3
determinantOutput= a(1,1) * (a(2,2) * a(3,3) - a(3,2)
* a(2,3))- a(1,2) * (a(2,1) * a(3,3) - a(3,1) * a(2,3)) + a(1,3)
* (a(2,1) * a(3,2) - a(3,1) * a(2,2));
else
determinantOutput= (a(1,1)* a(2,2) - a(1,2)*a(2,1));
end
I'm pretty new to MATLAB any help is greatly appreciated, thanks!

Answers (1)

BhaTTa
BhaTTa on 18 Oct 2024
Hey @William Brown, you can pass a matrix as an argument to the function, while calling the function from command line make sure you would call it as below:
>>myDeterminantCalc([1 2; 3 4])
Hope it helps.

Community Treasure Hunt

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

Start Hunting!