how to full square matrix first column as second column and so on

2 次查看(过去 30 天)
first column of full square matrix as second column and second column as third column
DG2 =
7.2263 36.234 41.867 36.555
0 43.326 49.059 36.53
0 0 6.9689 58.871
0 0 0 61.001
i need this because
h = view(biograph(DG2,[],'ShowWeights','on'))
Warning: Self connecting nodes are not allowed, ignoring the diagonal of CM.
> In biograph.biograph at 158
pls help me

回答(1 个)

Abhishek
Abhishek 2025-4-3
The issue that you are facing is because the MATLAB's ‘biograph’ function expects a connection matrix where only non-diagonal and positive entries indicate connections between nodes. The diagonal entries represent self-connections, which are not allowed in this context.
A workaround for this would be the following:
Shift the Columns: Move each column of your matrix one position to the right, filling the first column with zeros.
Here is the code to implement the same. I have tried it in MATLAB R2021a:
% Original matrix DG2
DG2 = [
7.2263, 36.234, 41.867, 36.555;
0, 43.326, 49.059, 36.53;
0, 0, 6.9689, 58.871;
0, 0, 0, 61.001
];
% Initialize a new matrix with zeros
DG2_shifted = zeros(size(DG2));
% Shift the columns to the right
DG2_shifted(:, 2:end) = DG2(:, 1:end-1);
% This step ensures the diagonal elements are zero to avoid self-loops
DG2_shifted(logical(eye(size(DG2_shifted)))) = 0;
% Create the biograph object without self-connections
h = view(biograph(DG2_shifted, [], 'ShowWeights', 'on'));
The above implementation ensures a correct representation of your data as a graph, adhering to the requirements of the biograph function. I have attached the output of the above implementation. You can follow the documentation for ‘biograph’ object and function to know more about its usage: https://www.mathworks.com/help//releases/R2021a/bioinfo/ref/biograph.html
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by