Home > Software design >  Matlab undefined variable or function "out"
Matlab undefined variable or function "out"

Time:05-01

I have this code in MATLAB:

function [out,f]  = rec_det(a,n)


if(n==2)

 out = a(1,1)*a(2,2)-a(2,1)*a(1,2);



else

for j=1:n
     f = a(1,j);
     a(1,:) = [];
     a(:,3) = [];
     out = out    ((-1)^(1 n)*f* rec_det(a,n-1));





end

end

And I get a error in this line" out = out ((-1)^(1 n)f rec_det(a,n-1));".The error says "undefined function of variable out" but I dont get why this is happening.

I'm very new to MATLAB so please be patient with me.

CodePudding user response:

This should work:

function [out,f]  = rec_det(a,n)


if(n==2)

out = a(1,1)*a(2,2)-a(2,1)*a(1,2);

else
s = 0;
for j=1:n
   f = a(1,j);
   a(1,:) = [];
   a(:,3) = [];
   s = s    ((-1)^(1 n)*f* rec_det(a,n-1));

end
out = s;
end
  • Related