# Need help with function for calculating the cofactors of a matrix in Matlab

on
Hello all,

I am trying to write a function for matlab to calculate the cofactors of a given matrix, until now I have written the following:

function C = co(A, i, j)
A = input('prompt: ');

if isempty(A)
error ='The promt is empty'
elseif nargin == 3
% Remove row i and column j to produce the minor.
A(i,:) = [];
A(:,j) = [];
C = (-1)^(i+j) * det(A);
else
[n,n] = size(A);
for i = 1:n
for j = 1:n
C(i,j) = co(A, i, j);
end
end
end

which results in promting me to put a 3x3 matrix a total of 10 times before i get the results. I would like to fix this issue and furthermore I would like to implement in the same function the ability after generating the minors (previous function) to be able to set the correct sign.  If the row and column of the element add up to be an odd number then the sign is the opposite, if not is the same
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
It is not a godd idea to put INPUT into a function. You are supposed to run function from script or GUI. Remove this line from the function, save the file as co.m. Then create another file, like co_test_script.m with the input prompt (if you want) and function call.

Commented:
do you define n someplace?

Commented:
Thank you for the advice. After the function is completed I will use it in a similar manner to the one you describe. I really struggle at the moment to implement the aforementioned Function to calculate the cofactors of a matrix.
Do you have any advice regarding the problems that I have to tackle?

Commented:
[n,n]  equals the size of A size(A).

else
[n,n] = size(A);
for i = 1:n

Commented:
How do you run this function? Do you put any arguments.
It will prompt to enter the matrix A anyway.
If you so insist using prompt in side the function try the following:

if ~exist('A','var') || isempty (A)
A = input('prompt: ');
elseif ...

Commented:
Thank you very much, but I would like to note that the way of inputing A is not related to the problem I am having. My problem is the following:

Why I have to iterate N times before I get the final result? Please try to execute the above code with the following matrix [2 5 6; 4 4 1; 2 5 3]

You will be promted 10 times to enter the same matrix and then give the result, why this happens? and how can I solve this problem?

Example:

prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]
prompt: [2 5 6; 4 4 1; 2 5 3]

ans =

7   -10    12
15    -6     0
-19    22   -12
Commented:
Every time the function is called it prompts for A. First time you call it in the beginning of the function, then 9 times in the loop. Try the code below.
The solution for your problem is IF statement around input.

What about if you enter non-square matrix? I put this check and also preallocation for C. You can remove it if you want.
function C = co(A, i, j)
if ~exist('A','var')
A = input('prompt: ');
end
if isempty(A)
error ('The prompt is empty')
elseif nargin == 3
% Remove row i and column j to produce the minor.
A(i,:) = [];
A(:,j) = [];
C = (-1)^(i+j) * det(A);
else
[n,m] = size(A);
if n~=m
error('Non-square matrix');
end
C=zeros(size(A)); % preallocation
for i = 1:n
for j = 1:m
C(i,j) = co(A, i, j);
end
end
end

Do more with