How do I generate a node graph of Matrix of resistors with a given pattern.
1 view (last 30 days)
Show older comments
I have made the code for making the node graph as shown below. (Don't concentrate on resistances, they are going to be connected by an external electronic simulator later in the code which I have not mentioned here). I have fabricated this code such that it asks how many numbers of domains (cross-resistance patterns) user needs and it generates the node graph. The MATLAB code works pretty fine when the number of domains is 25x25 or lesser. When I increase it like 40x40 or even higher like 100x100, the time taken to generate this matrix is exponentially increasing. It takes hours, I am not good at coding and hence this code below is just all nested loops. I am sure that there might be other clever ways to do this in the shortest time, The time taking instances start from '%% removing left and right edge resistances of the matrix'. Please let me know your inputs. Thank you.
%% Array dimensions
Domains_X = 50; %Set the X size of the matrix
SizeX = 2*Domains_X + 1;
Domains_Y = 50; %Set the Y size of the matrix
SizeY = 2*Domains_Y + 1;
Position = 1; %First starting point of the array
ArrayPosition = 1; %Counter for the Array
Sink(:,1) = [SizeX*SizeY-SizeX+1:1:SizeX*SizeY]; %This is going to be the node that is grounded
Source(:,1) = [1:1:SizeX]; %This is going to be the node that we apply a voltage on
%% Array formation
for Y = 1:1:SizeY
for X = 1:1:SizeX
if X ~= SizeX && Position >= SizeX && Position <= SizeX*SizeY-SizeX %Keeps the array in a uniform box X limit
VO2Array(ArrayPosition,1) = Position; %Stores the starting position in the array
VO2Array(ArrayPosition,2) = Position + 1; %Stores the node next to starting position
end
if Y ~= SizeY %Keep the array in a uniform box Y limit
VO2Array(ArrayPosition + 1,1) = Position; %Stores the starting positon in the array
VO2Array(ArrayPosition + 1,2) = Position + SizeX; %Stores the node below(Next row) the starting array
end
Position = Position + 1; %Go to the next node
ArrayPosition = ArrayPosition + 2; %Positon of the array
end
end
Zero = find(VO2Array(:,1) == 0); %Find any zero rows, this was made because it was keeping the box array uniform
VO2Array(Zero,:) = []; %Remove all zero rows
% Graph = graph(VO2Array(:,1),VO2Array(:,2));
% plot_array = plot(Graph,'layout', 'auto');
% plot_array.NodeColor = 'red';
%% Gaussian distribution for Ron, Roff, and Vt
RmeanON = 1000; %Metallic state of VO2
RSTDON = 100; %Standard deviation from on mean
RmeanOFF = 10000000; %Insulating state of VO2
RSTDOFF = 1000000; %Standard deviation from off mean
%Using a normal distribution give each arc a Ron, Roff, and turn voltage
VO2Array(:,3) = abs(normrnd(RmeanON,RSTDON,size(VO2Array,1),1));
VO2Array(:,4) = abs(normrnd(RmeanOFF,RSTDOFF,size(VO2Array,1),1));
Arcs = size(VO2Array,1); %Number of arcs in the array, this is for counting
%% removing left and right edge resistances of the matrix
fprintf('\nRemoving left and right edge resistances\n');
left_edge_matrix(:,1) = [Source(1,1):size(Source,1):Sink(1,1)];
for i = 1:1:size(VO2Array,1)
for j = 1:1:size(left_edge_matrix,1)-1
if (VO2Array(i,1) == left_edge_matrix(j,1) && VO2Array(i,2) == left_edge_matrix(j+1,1))
VO2Array(i,3:4) = inf;
elseif (VO2Array(i,1) == left_edge_matrix(j+1,1) && VO2Array(i,2) == left_edge_matrix(j,1))
VO2Array(i,3:4) = inf;
end
end
end
right_edge_matrix(:,1) = left_edge_matrix + (SizeX - 1)*ones(size(left_edge_matrix,1),1);
for i = 1:1:size(VO2Array,1)
for j = 1:1:size(right_edge_matrix,1)-1
if (VO2Array(i,1) == right_edge_matrix(j,1) && VO2Array(i,2) == right_edge_matrix(j+1,1))
VO2Array(i,3:4) = inf;
elseif (VO2Array(i,1) == right_edge_matrix(j+1,1) && VO2Array(i,2) == right_edge_matrix(j,1))
VO2Array(i,3:4) = inf;
end
end
end
%% removing unwanted resistances to make a cross domain
fprintf('\nRemoving unwanted resistances\n');
% remove domains connected to corners
for i = 1:1:size(VO2Array,1)
if ((isempty(find(VO2Array(i,1:2) == Source(1,1))) == 0) || (isempty(find(VO2Array(i,1:2) == Source(size(Source,1),1))) == 0)) %Ground in Xyce is denoted as 0 if we come across a node that is the sink we need to relabel as node 0
VO2Array(i,3) = R_max;
VO2Array(i,4) = R_max;
end
if ((isempty(find(VO2Array(i,1:2) == Sink(1,1))) == 0) || (isempty(find(VO2Array(i,1:2) == Sink(size(Sink,1),1))) == 0)) %Ground in Xyce is denoted as 0 if we come across a node that is the sink we need to relabel as node 0
VO2Array(i,3) = R_max;
VO2Array(i,4) = R_max;
end
end
% remove redundant VERTICAL domains connected
for i = 1:1:size(VO2Array,1)
for j = Source(1,1)+2:2:size(Source,1)-2
for k = 1:1:SizeY-1
if ((isempty(find(VO2Array(i,1:2) == j+(k-1)*size(Source,1))) == 0) && (isempty (find (VO2Array(i,1:2) == (Source(j,1) + k*size(Source,1))) ) == 0)) %Ground in Xyce is denoted as 0 if we come across a node that is the sink we need to relabel as node 0
VO2Array(i,3) = R_max;
VO2Array(i,4) = R_max;
end
end
end
end
% remove redundant HORIZONTAL domains connected
for i = 1:1:size(VO2Array,1)
for j = left_edge_matrix(1,1)+2*size(Source,1):2*size(Source,1):left_edge_matrix(size(left_edge_matrix,1),1)-2
for k = 1:1:SizeX-16
if ((isempty(find(VO2Array(i,1:2) == j+(k-1))) == 0) && (isempty (find(VO2Array(i,1:2) == (j + k))) == 0)) %Ground in Xyce is denoted as 0 if we come across a node that is the sink we need to relabel as node 0
VO2Array(i,3) = R_max;
VO2Array(i,4) = R_max;
end
end
end
end
fprintf('\nRemoving unwanted resistances ends\n');
0 Comments
Accepted Answer
Steven Lord
on 30 Nov 2019
So you just want a rectangular grid of nodes with each connected to its neighbors above, below, left, and right except for nodes on the edges?
M = reshape(1:30, [6 5]);
G = graph(M(:, 1:end-1), M(:, 2:end), 1);
G = addedge(G, M(1:end-1, :), M(2:end, :), 1);
plot(G)
You can go in and fill in the Weight column of the Edges table in the graph G with the values from your simulation. The findedge function may be useful in this as may the degrees and neighbors functions (if the resistance values is related to how many resistors are connected to a given node, for example.)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!