'm a beginner who learns to make graphs manually, can this writing be changed using a for loop to make it look simpler

1 view (last 30 days)
clear all
x_max = 30;
y_max = 30;
% Define cordinate of node 8 x 8 / n x n
nodes = [1 1; 2 1; 3 1; 4 1; 5 1; 6 1; 7 1; 1 8;
1 2; 2 2; 3 2; 4 2; 5 2; 6 2; 7 2; 8 2;
1 3; 2 3; 3 3; 4 3; 5 3; 6 3; 7 3; 8 3;
1 4; 2 4; 3 4; 4 4; 5 4; 6 4; 7 4; 8 4;
1 5; 2 5; 3 5; 4 5; 5 5; 6 5; 7 5; 8 5;
1 6; 2 6; 3 6; 4 6; 5 6; 6 6; 7 6; 8 6;
1 7; 2 7; 3 7; 4 7; 5 7; 6 7; 7 7; 8 7;
1 8; 2 8; 3 8; 4 8; 5 8; 6 8; 7 8; 8 8];
%make all node value 0
A = zeros(size(nodes,1), size(nodes,1));
%the connected node has a value of 1 in the Adjacency Matric
A(10,11) = 1;
A(10,18) = 1;
A(11,12) = 1;
A(11,19) = 1;
A(12,13) = 1;
A(12,20) = 1;
A(13,21) = 1;
A(21,20) = 1;
A(20,12) = 1;
A(20,19) = 1;
A(19,11) = 1;
A(19,18) = 1;
A(18,10) = 1;
A(21,29) = 1;
A(29,37) = 1;
A(37,45) = 1;
A(45,53) = 1;
A(53,52) = 1;
A(52,51) = 1;
A(51,50) = 1;
A(51,43) = 1;
A(50,42) = 1;
A(43,35) = 1;
A(42,34) = 1;
A(35,34) = 1;
A(53,54) = 1;
A(54,55) = 1;
A(55,47) = 1;
A(47,39) = 1;
A(39,31) = 1;
A(39,12) = 1;
%Define the cost matrix
C = ones(size(nodes,1), size(nodes,1));

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 11 Jun 2022
nodes = ...
[1 1; 2 1; 3 1; 4 1; 5 1; 6 1; 7 1; 1 8; % <<== may be the last "1 8;" is typo
1 2; 2 2; 3 2; 4 2; 5 2; 6 2; 7 2; 8 2;
1 3; 2 3; 3 3; 4 3; 5 3; 6 3; 7 3; 8 3;
1 4; 2 4; 3 4; 4 4; 5 4; 6 4; 7 4; 8 4;
1 5; 2 5; 3 5; 4 5; 5 5; 6 5; 7 5; 8 5;
1 6; 2 6; 3 6; 4 6; 5 6; 6 6; 7 6; 8 6;
1 7; 2 7; 3 7; 4 7; 5 7; 6 7; 7 7; 8 7;
1 8; 2 8; 3 8; 4 8; 5 8; 6 8; 7 8; 8 8];
This part can be changed to below by using for loop.
for i = 0:63
nodes(i + 1, :) = [mod(i,8), fix(i/8)] + 1;
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!