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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by