vectorization
    6 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
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
Akzeptierte Antwort
  Dr. Seis
      
 am 8 Dez. 2011
        If A is an NxN matrix:
A(1:N+1:N*N) = 0;
1 Kommentar
  Dr. Seis
      
 am 8 Dez. 2011
				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
Weitere Antworten (1)
  Sean de Wolski
      
      
 am 8 Dez. 2011
        One way:
A = magic(100); %sample matrix
A(logical(speye(length(A)))) = 0;
Also diag if you're building a matrix
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!