how to full fill the diagonal of a matrix by a vector
    12 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
hello
how can i full fill a matrix (17*9) by a vector by length 9
for example i have the vector d=[1 2 3 ] and the matrix zeros(5*5) and i want to make the output matrix like this
1 0 0 3 2
2 1 0 0 3
3 2 1 0 0
0 3 2 1 0
0 0 3 2 1
thanks
0 commentaires
Réponse acceptée
  Adam Danz
    
      
 le 8 Mai 2019
        m = zeros(5,5); 
v = [1,2,3]; 
% Loop through each column of m
for i = 1:size(m,2)
    m(1:length(v),i) = v; 
    m(:,i) = circshift(m(:,1), i-1);
end
Result:
m =
     1     0     0     3     2
     2     1     0     0     3
     3     2     1     0     0
     0     3     2     1     0
     0     0     3     2     1
2 commentaires
Plus de réponses (3)
  Geoff Hayes
      
      
 le 8 Mai 2019
        Ahmed - try using
a = [1 2 3 0 0]';
A = cell2mat(arrayfun(@(x)circshift(a,x-1),1:length(a), 'UniformOutput', false));
2 commentaires
  Matt J
      
      
 le 10 Mai 2019
        You can use interpMatrix from the File Exchange
>> full(interpMatrix([1,2,3],1,5,1,'circ'))
ans =
     1     0     0     3     2
     2     1     0     0     3
     3     2     1     0     0
     0     3     2     1     0
     0     0     3     2     1
0 commentaires
Voir également
Catégories
				En savoir plus sur Operating on Diagonal Matrices 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!




