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

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

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

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

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

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

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial