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

1 view (last 30 days)
ANDREW B I
ANDREW B I on 25 Feb 2021
Answered: Dongyue on 17 Nov 2022
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))

Answers (2)

Avinash
Avinash on 14 Oct 2022
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 on 17 Nov 2022
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

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!