% Code is for Octave
%-----------------------------------%
%                                   %
% (C) 2014 - Michael K. Pellegrino  %
%                                   %
% My implementation of a matrix     %
% to cofactor matrix calculator     %
%                                   %
% usage: cofactor( M )              %
% where M is any square matrix      %
%                                   %
%-----------------------------------%
function ret=cofactor( M )
  [nrows, ncols]=size( M );
  if( nrows != ncols )
    disp( "[EROR: MUST BE SQUARE MATRIX]" );
    return;
  endif
  ret=M;
  for i = 1:nrows
    for j = 1:ncols
	T=M;
	T( :, [ j ] ) = [];
	T( [i],: ) = [];
	ret(i,j)=det(T)*(-1)^(i-1)*(-1)^(j-1);
    endfor
  endfor
endfunction
