why in this function they use empty square brackets?

13 ビュー (過去 30 日間)
Almog Na
Almog Na 2021 年 11 月 13 日
コメント済み: Almog Na 2021 年 11 月 14 日
and how exactly this is solving the determinant, I don't completley understand what they did here/
sorry if it's a dumb question I'm new to coding and to matlab.
function det = myDet(A)
if isscalar(A)
det = A;
return
end
det = 0;
top_row = A(1,:);
A(1,:) = [];
for i = 1:size(A,2) % number of columns
A_i = A;
A_i(:,i) = [];
det = det+(-1)^(i+1)*top_row(i)*myDet(A_i);
end
end

採用された回答

John D'Errico
John D'Errico 2021 年 11 月 13 日
編集済み: John D'Errico 2021 年 11 月 13 日
It is not a dumb question at all, because the syntax used by MATLAB to delete a row or column of an array may not be obvious. But the best way, as others have said, is to try it out. See what happens to the array when you do that. Getting your hands dirty is the best way to learn.
A = magic(3)
A = 3×3
8 1 6 3 5 7 4 9 2
% first, I'll delete the second row of A
A(2,:) = []
A = 2×3
8 1 6 4 9 2
% Next, delete the third column of that matrix.
A(:,3) = []
A = 2×2
8 1 4 9

その他の回答 (2 件)

Cris LaPierre
Cris LaPierre 2021 年 11 月 13 日
In this context, it appears to be deleting elements from A.
Try it out in MATLAB to see what it is doing.
A = rand(3)
A = 3×3
0.0943 0.6034 0.8591 0.6091 0.1028 0.2550 0.9727 0.2310 0.9515
A(1,:)=[]
A = 2×3
0.6091 0.1028 0.2550 0.9727 0.2310 0.9515

Jan
Jan 2021 年 11 月 13 日
編集済み: Jan 2021 年 11 月 13 日
A(i,:) = [];
This deletes the i.th row of the matrix A.
Example:
A = [1,2,3; 4,5,6; 7,8,9];
A(1, :) = []
% A = [4,5,6; 7,8,9]

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by