how can i write this matrix in matlab?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
how can i write this matrix in matlab? this matrix is (n+1)*(n+1) that n must be enter by user
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145932/image.jpeg)
0 commentaires
Réponse acceptée
Rick Rosson
le 2 Nov 2014
x = zeros(n+1);
x(1,1:2) = [ 2 1 ];
x(end,end-1:end) = [ 1 2 ];
for k = 2:n
x(k,k–1:k+1) = [ 1 4 1 ];
end
0 commentaires
Plus de réponses (2)
dpb
le 2 Nov 2014
Given input n and
r = [1 4 1];
c={repmat(r,n,1)}; % cell array of n copies of r
m=blkdiag([2 1],c{:},[1 2]); % expand c to comma list for blkdiag
clear c
0 commentaires
Image Analyst
le 2 Nov 2014
Lots of ways, for loop, addition, assignment, zeros(), etc. I'd probably use eye() and circshift(). Hint:
diagArray = 4 * eye(7)
See what that gives you. Imagine shifting some diagonal arrays with circhsift() and adding the 3 arrays together. That should be enough hint to get you started.
1 commentaire
dpb
le 3 Nov 2014
I thought the blkdiag solution kinda' cute, IA... :)
Excepting for needing the temporary cell array; couldn't see a simple way to generate the comma-separated list in one swell foop...
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!