# Replace specific/diagonal elements in Matrix using editor, scripting

10 vues (au cours des 30 derniers jours)
Vincent Hodnett le 6 Jan 2020
Hello,
Looking a simple way to replace diagonal values in vector to 0. The matrix is a variance-covariance matrix, however in system will be assuming covariance are
zero, and are independent to each. Have the following matrix:
A = [ 2.5 0.25; 0.25 2.4]
B = flip(A) %Flipped to get in correct orientation to select diagonals 0.25, 0.25
%Not sure if it is possible to select mirrored diagonal in original matrix A
Covariance values are 0.25, which would like to set to zero. However these value could change, therefore if had method to replace these diagonal values with respect to m-row, n-column position, or other method?
If can be selected from original matrix A, would be ideal to avoid flipping.
kind rgds
##### 2 commentairesAfficher AucuneMasquer Aucune
Max Murphy le 6 Jan 2020
Will your system always be a bivariate distribution or will A ever be larger than 2x2?
Vincent Hodnett le 6 Jan 2020
Modifié(e) : Vincent Hodnett le 6 Jan 2020
Yes, for this instance. Shall only be 2x2 matrix.Thks

Connectez-vous pour commenter.

### Réponse acceptée

Max Murphy le 6 Jan 2020
% If this is to be called iteratively
% Example while loop
iteration = 0;
while (iteration < numel(data)) % Some conditional to stay in your loop
if ~iteration
% Initially
% A = [2.5 0; 0 2.4]; % Can you just initialize it with zeros?
% Alternatively, suppose it was imported elsewhere:
A = [ 2.5 0.25; ... % Top right = A(1,2);
0.25 2.4]; % Bottom left = A(2,1);
A = diag(diag(A)); % Keep only diagonal elements
else
A(1,2) = cov_ab; % Assume you get this from elsewhere
A(2,1) = cov_ba;
end
...
% Get covariance values
doc cov % May be helpful
...
iteration = iteration + 1;
end
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Vincent Hodnett le 6 Jan 2020
Modifié(e) : Vincent Hodnett le 6 Jan 2020
Hello Max,
Thanks, for advice. Yes, the A = diag(diag(A)) will solve issue. To keep only diagonal element
A = [ 2.5 0.25; 0.25 0.78];
A = diag(diag(A))
% This will give the matrix that is needed, 0.25 and 0.25 are equal to 0 and we have kept only %the variance, diagonal. :) :)
Thanks,
kind regards Vincent

Connectez-vous pour commenter.

### 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!

Translated by