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

eikei
eikei used Ask the Experts™
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
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.
do you define n someplace?

Author

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?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

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 ...

Open in new window

Author

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

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial