フィルターのクリア

vectorization

2 ビュー (過去 30 日間)
Steven
Steven 2011 年 12 月 8 日
hi,
is there a quick way to change the values of the diagonal of a matrix, instead of
for i = 1:length(A)
A(i,i) = 0;
end
thx
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 12 月 8 日
A-diag(diag(A))

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

採用された回答

Dr. Seis
Dr. Seis 2011 年 12 月 8 日
If A is an NxN matrix:
A(1:N+1:N*N) = 0;
  1 件のコメント
Dr. Seis
Dr. Seis 2011 年 12 月 8 日
N = 10000;
A = rand(N);
tic
A(1:N+1:N*N)=0;
toc % Took 0.000532 seconds
A = rand(N);
tic
A(logical(speye(length(A)))) = 0;
toc % Took 0.001379 seconds
A = rand(N);
tic
A = A - diag(diag(A));
toc % Took 0.215796 seconds

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 12 月 8 日
One way:
A = magic(100); %sample matrix
A(logical(speye(length(A)))) = 0;
Also diag if you're building a matrix

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by