Changing elements in a MxN matrix
    6 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    WARRIOR24
 le 20 Avr 2021
  
    
    
    
    
    Modifié(e) : per isakson
      
      
 le 20 Avr 2021
            I am trying to get zeros for certain part of a Matrix to zeros
I need this matrix ones that are bolded, underlines, italic "1"  to  become zeros. I have been trying to figure this out for a while. I tried if statesments for row > 5 and for loops. 
4	1	0	1	0	1	0	1	0	1
1	4	1	0	1	0	1	0	1	0
0	1	4	1	0	1	0	1	0	1
1	0	1	4	1	0	1	0	1	0
0	1	0	1	4	1	0	1	0	1
1	0	1	0	1	4	1	0	1	0
0	1	0	1	0	1	4	1	0	1
1	0	1	0	1	0	1	4	1	0
0	1	0	1	0	1	0	1	4	1
1	0	1	0	1	0	1	0	1	4
Here is my code:
inverse = diag(ones(10,1));
inverse = inverse*4;
for col = 1:length(domain-1)
    for row = 1:10
       if row == col
            inverse(row+1,col) = 1;
            inverse(row,col+1) = 1;
       elseif mod(row+col,2) == 1
            inverse(row,col) = 1;
       end
    end
end
matrix = inverse(Nx-1,Ny-1)
0 commentaires
Réponse acceptée
  per isakson
      
      
 le 20 Avr 2021
        
      Modifié(e) : per isakson
      
      
 le 20 Avr 2021
  
      Does this help? 
%%
matrix  = magic(10);
matrix = triu( matrix,-4 );
matrix = tril( matrix, 4 )
%% This is better, it only overwrites the specific diagonals
matrix  = magic(10);
for k = [5,7,9]
    isdiag = diag( true(10-k,1), k );
    matrix( isdiag ) = 0;
    isdiag = diag( true(10-k,1), -k );
    matrix( isdiag ) = 0;
end
matrix
I was fooled by your example and used a 10x10 matrix in my demos. Your title says MxN matrix.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Linear Algebra dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

