How to Assemble the matrix shown in the image on Matlab for any number of nodes?

6 vues (au cours des 30 derniers jours)

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Jan 2016
n = 4; %for example
main_diagonal = [ [-4, repmat(-3,1,n-2), -4], repmat([-5, repmat(-4,1,n-2), -5], 1, 2), [-6, repmat(-5,1,n-2), -6] ];
N = length(main_diagonal);
m = diag(ones(1, N-4), -4) +
diag(ones(1, N-1), -1) +
diag(main_diagonal) +
diag(ones(1, N-1), 1) +
diag(ones(1, N-4), -4)

Plus de réponses (1)

Guillaume
Guillaume le 27 Jan 2016
There is not enough information to know how to construct the main diagonal, but you can just sum a bunch of diag:
m = diag(ones(1, 12), -4) + ...
diag(ones(1, 15), -1) + ...
diag([-4 -3 -3 -4 -5 -4 -4 -5 -5 -4 -4 -5 -6 -5 -5 -6], 0) + ... %no idea what the pattern is
diag(ones(1, 15), 1) + ...
diag(ones(1, 12), 4)
  2 commentaires
amar
amar le 27 Jan 2016
I want to assemble this matrix for any number of nodes 'n'
Walter Roberson
Walter Roberson le 27 Jan 2016
main_diagonal = [-4 -3 -3 -4 -5 -4 -4 -5 -5 -4 -4 -5 -6 -5 -5 -6]; %we do not know the rule for this
n = length(main_diagonal);
m = diag(ones(1, n-4), -4) +
diag(ones(1, n-1), -1) +
diag(main_diagonal) +
diag(ones(1, n-1), 1) +
diag(ones(1, n-4), -4)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by