How to form a matrix from a coding of a graph?

1 回表示 (過去 30 日間)
ANDREW B I
ANDREW B I 2021 年 2 月 25 日
回答済み: Dongyue 2022 年 11 月 17 日
I have coding of a graph (vertices and edges) to find characteristic polynomial and eigen values. I need to view them in matrix form. Do we have coding to form a matrix?
clc;
clear all;
n = input('Enter the order of the inner path n = ');
m = 2*n+2
% undirected tree
T_1 = zeros(m);
T_1(1,2) = 1;
for i = 2 : n-1
T_1(i,i+1) = 1;
T_1(i,n+i-1) = 1;
T_1(i,(2*n)+i-3) = 1;
end
for i = 1 : m-1
for j = i+1 : m
T_1(j,i) = T_1(i,j);
end
end
% disp(T_1);
% characteristic polynomials
C_1 = charpoly(T_1);
disp(C_1)
% Eigenvalues of all
disp(eig(T_1))

回答 (2 件)

Avinash
Avinash 2022 年 10 月 14 日
Hi Andrew,
You can store the output in the matrix form using a for loop or a reshape function.
As like you have filled the values into the input matrix, you can also fill the values of output into a matrix. You can go through the following link of one such example:
You can also go through documentation page of reshape function and look at the examples
Regards
Avinash

Dongyue
Dongyue 2022 年 11 月 17 日
Hi Andrew,
If you want to reshape the vector into matrix, you can use function reshape(), you can go through the documentation of reshape function with the following link:
if you already know the dimensions of the expected results, you can pre-allocate a empty matrix using functions zeros() or ones(), and then fill values into the matrix. You can go through the documentation of zeros/ones function with the following link:
if you want to combine your results into one matrix in stack, you can also use cat() function, but you need to make sure the dimension of your results are consistent. For more information of cat function, please go through the link below:
Best,
Dongyue

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by