I need to know a function to calculate the cofactor of a matrix, thank a lot!

7 件のコメント

Quilee Simeon
Quilee Simeon 2018 年 8 月 21 日
cofactor matrix for a matrix A is just det(A)*inv(A)
Zoe Herrick
Zoe Herrick 2018 年 9 月 14 日
編集済み: Walter Roberson 2018 年 9 月 15 日
det(A)*inv(A) gives the adjugate or classical adjoint of matrix A which is the Transpose of the cofactor matrix.
This wiki article gives a brief layout of this:
Franco Salcedo Lópezz
Franco Salcedo Lópezz 2019 年 11 月 14 日
Here I leave this code, I hope it helps. Regards..
function v = adj(M,i,j)
t=length(M);
v=zeros(t-1,t-1);
ii=1;
ban=0;
for k=1:t
jj=1;
for m=1:t
if ( (i~=k)&&(j~=m) )
v(ii,jj)=M(k,m);
jj++;
ban=1;
endif
endfor
if(ban==1)ii++;ban=0;endif
endfor
Walter Roberson
Walter Roberson 2020 年 2 月 6 日
ii++ is not valid MATLAB though. And endif and endfor are not MATLAB either.
Fernando Salinas
Fernando Salinas 2020 年 11 月 10 日
I wrote this in GNU/Octave but I guess it should work on MATLAB
function cofactor = matrizCofactores(A)
[rows, cols] = size(A);
if rows == cols
for i = 1 : rows,
for j = 1 : cols,
Menor = A;
Menor(i,:) = [];
Menor(:,j) = [];
if mod((i+j),2) == 0
cofactor(i,j) = det(Menor);
else
cofactor(i,j) = -det(Menor);
endif
endfor
endfor
endif
endfunction
Natasha St Hilaire
Natasha St Hilaire 2021 年 10 月 7 日
What is "menor" short for?
Walter Roberson
Walter Roberson 2021 年 10 月 8 日
I suspect that the English word would be "minor". The Spanish word "menor" can be translated as English "minor" in some situations.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2012 年 2 月 2 日

1 投票

10 件のコメント

Mariana
Mariana 2012 年 2 月 2 日
Walter, thanks for your answer.. Where I have to save the function (.m) and what name i must to give to the file (.m)?
Walter Roberson
Walter Roberson 2012 年 2 月 3 日
Save the file in any directory on your matlab path, or save it in a directory and use the path tool to add that directory to your matlab path.
Name the file anything that is convenient and which is a valid matlab variable name (starts with a letter, letters or digits or underscore after, 63 characters max, is not a reserved word). Naming it the same thing as another existing MATLAB routine would usually cause Problems.
As there is no MATLAB cofactor command, naming it cofactor.m would seem appropriate.
Mariana
Mariana 2012 年 2 月 3 日
Walter, when I save the file appear a message saying that I don't have permissions to do that.. and when I try to add it appear a message too and don't let me add. Thank you!
Mariana
Mariana 2012 年 2 月 3 日
But anyway the code is working and given the cofactor without saving.. I just wanna know why I can't save the function, thanks
Walter Roberson
Walter Roberson 2012 年 2 月 3 日
編集済み: Walter Roberson 2024 年 12 月 26 日
Mariana
Mariana 2012 年 2 月 7 日
Walter,
I get it, Im using Windows 7 and a Matlab 7.12, I just click in a green icon on the Editor that say RUN, and after appear a windows asking to add to a PATH.. and say YES ... I thing the code is in the livrary now.. and it's working so fine.. thanks a lot
Mariana
Mariana 2012 年 2 月 7 日
Yes.. this work, but when I close Matlab and open again.. not work
Walter Roberson
Walter Roberson 2012 年 2 月 7 日
Did you try the solutions in the second link?
Mariana
Mariana 2012 年 2 月 7 日
Yes, I right-click on the shortcut and select to run as administrator. And I save the function on the lib file, and the function work with matrix.. But when I close and open again the function when I try to use the function a message say that it is Undefined..
Mariana
Mariana 2012 年 2 月 7 日
Walter,
I thing I get it.. I forget to add the function to the PATH through the SET PATH in the menu file.. Thank you very much for all..
Just one question more.. Matlab run in linux? what distribution is better?

サインインしてコメントする。

その他の回答 (2 件)

Dr. Murtaza Ali Khan
Dr. Murtaza Ali Khan 2019 年 9 月 28 日

3 投票

A = [
2 4 1
4 3 7
2 1 3
]
detA = det(A)
invA = inv(A)
cofactorA = transpose(detA*invA)

2 件のコメント

Franco Salcedo Lópezz
Franco Salcedo Lópezz 2019 年 11 月 14 日
編集済み: Franco Salcedo Lópezz 2019 年 11 月 14 日
Here I leave this code, I hope it helps. Regards
function v = adj(M,i,j)
t=length(M);
v=zeros(t-1,t-1);
ii=1;
ban=0;
for k=1:t
jj=1;
for m=1:t
if ( (i~=k)&&(j~=m) )
v(ii,jj)=M(k,m);
jj++;
ban=1;
endif
endfor
if(ban==1)ii++;ban=0;endif
endfor
Walter Roberson
Walter Roberson 2021 年 10 月 11 日
This is not MATLAB code. It might be Octave.

サインインしてコメントする。

Francisco Trigo
Francisco Trigo 2020 年 2 月 6 日

0 投票

The matrix confactor of a given matrix A can be calculated as det(A)*inv(A), but also as the adjoint(A). And this strange, because in most texts the adjoint of a matrix and the cofactor of that matrix are tranposed to each other. But in MATLAB are equal. I found a bit strange the MATLAB definition of the adjoint of a matrix.

1 件のコメント

Zuhri Zuhri
Zuhri Zuhri 2021 年 9 月 28 日
adjoint matrix is ​​the transpose of the cofactor matrix so the above result is correct

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

質問済み:

2012 年 2 月 2 日

編集済み:

2024 年 12 月 26 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by