how can i negative all value in a matrix except diagonal?
    8 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    arian hoseini
 il 11 Gen 2022
  
    
    
    
    
    Modificato: Stephen23
      
      
 il 11 Apr 2022
            13.333            5            0            5       3.3333            0
            5       27.333            4           10       3.3333            5
            0            4       17.846            0       3.8462           10
            5           10            0         17.5          2.5            0
       3.3333       3.3333       3.8462          2.5       16.346       3.3333
            0            5           10            0       3.3333       18.333
Risposta accettata
  Stephen23
      
      
 il 11 Gen 2022
        
      Modificato: Stephen23
      
      
 il 11 Apr 2022
  
      M = [13.333,5,0,5,3.3333,0;5,27.333,4,10,3.3333,5;0,4,17.846,0,3.8462,10;5,10,0,17.5,2.5,0;3.3333,3.3333,3.8462,2.5,16.346,3.3333;0,5,10,0,3.3333,18.333]
Method one: subtraction (square only):
A = diag(diag(M)) - M.*~eye(size(M))
Method two: indexing (more robust, should work for any size):
B = -M;
B(eye(size(M))==1) = diag(M)
Più risposte (1)
  Jon
      
 il 11 Gen 2022
         Am = -(A - diag(diag(A))) + diag(diag(A))
2 Commenti
  Jon
      
 il 11 Gen 2022
				You could also do it in two lines using
Am = -A
Am(1:7:end) = A(1:7:end)
For both approaches, thanks to earlier posts from @John D'Errico https://www.mathworks.com/matlabcentral/answers/393125-how-to-replace-the-diagonal-entries-of-a-square-matrix-with-entries-from-a-vectore-of-equal-length
and
Vedere anche
Categorie
				Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



