MATLAB Answers

Average matrix values across diagonal

13 ビュー (過去 30 日間)
Johanna Popp
Johanna Popp 2021 年 6 月 28 日
コメント済み: Johanna Popp 2021 年 6 月 28 日
Hi all :)
I have an asymmetric structural connectivity matrix size 376 by 376 where the elements Cij and Cji are different. What I am trying to do is to average the values contained in Cij and Cji so that I have a symmetric matrix. Sp basically I am trying to average values "across the diagonal" of my matrix.
Any help would be appreciated!
Thanks :)

回答 (1 件)

Asmit Singh
Asmit Singh 2021 年 6 月 28 日
Hey, I think this is what you require. This basically takes a mean over the diagonally opposite numbers, to create a symmetrical matrix.
% replace with the needed matrix
matrix = [1,2,3 ; 4,5,6 ; 7,8,9];
display(matrix)
% row/column size
siz = size(matrix);
n = siz(1);
for i= 1:n
for j = i:n
matrix(i,j) = (matrix(i,j)+matrix(j,i))/2;
matrix(j,i) = matrix(i,j);
end
end
display(matrix);
  1 件のコメント
Johanna Popp
Johanna Popp 2021 年 6 月 28 日
awesome, thank you so much!

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by