**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Using viscircles to draw circles around points.

11 views (last 30 days)

Show older comments

Vance Blake
on 23 Aug 2019

Hello, I am trying to draw to circles using viscircles but some of the circles end up in random space instead of around the center coordiantes ive given them. Below is the code I am using to draw my circles and a picture of what is happening on the plot. Any help would be greatly appreciated

% draw exlcusion range circles b/w hormone seeds and hormone seeds

HS_kept_x = HS_kept(:, 1);

HS_kept_y = HS_kept(:, 2);

LengthHS_kept = length(HS_kept);

radii_node2 = 8;

hold on

for i = 1:n

centers_node5 = [x(i), y(i)];

elim_circles3(i) = viscircles(centers_node5, radii_node2, 'color', 'k', 'linestyle', '--');

end

for j = 1:LengthHS_kept

centers_HS_kept = [HS_kept_x(j), HS_kept_y(j)];

elim_circles4(j) = viscircles(centers_HS_kept, radii_node2, 'color', 'k', 'linestyle', '--');

end

##### 20 Comments

Adam Danz
on 23 Aug 2019

We don't have enough information. The problem is clearly with the center points of the circles. Center points are defined by x,y, HS_kept_x, and HS_kept_y but we don't have those values (along with some other variables). Could you provide a mat file that contains all the needed variables?

One red-ish flag that often causes problems is the use of length() instead of numel() or size(a,b). With length() you can't be sure which dimension it will return if the input is a matrix that potentially varies in size.

Another possibility is that your code is working fine but the inputs are the problem.

Adam Danz
on 24 Aug 2019

Again, that's not enough information. Remember that we don't know what "t' 'r' 'x' 'y' are or how they relate to your code. The x and y variables are also in your code so I suppose they are the same x and y variables in the txt files but there's still not enough info to run your code and troubleshoot the problem.

What we need is something we can download (or copy) and then run it without having to guess how you're loading the data, what variable names you're assigning or the form they take in your code, etc.

Vance Blake
on 24 Aug 2019

Edited: Vance Blake
on 25 Aug 2019

Alright i understand. So Im plotting the points on a circular plot. I generate and plot the points in the shape of a smaller circle using the equations below but right now Im just working with a the data sets ive already attached. I use the points generated (hormone seeds HS) to grow out from the center vein [0,0] of the plane according to min distance from all the vein nodes (VN) and vector addition to plot the next vein node. All the variable names are mainly working with the x and y coordinates. I eliminate the HS that fail certain criteria and use the remaining ones to calculate the location of the next vein node (VN). Im trying to model and display all the steps and calculations of growing new vein nodes. The end goal is to have an algorithm that can run for any random set of points geneated but i need to document steps so ive been saving data sets and reading them into the code. Sorry for the incomplete examples.

t = 2*pi*rand(n,1);

r = r*sqrt(rand(n,1));

x = r.*cos(t); % x coordinates

y = r.*sin(t); % y coordinates

DataTable = importdata('CTRDataTable_HS_gen1.txt');

n = 10; % total number of points

q = 2; % pause time

fontsize = 12;

axis([-100, 100 -100 100]);

% iterations = 3; % total number of loops

% Axis Labels

xlabel('X', 'Fontsize', fontsize);

ylabel('Y', 'Fontsize', fontsize);

title('Constructal Theory Leaf Modeling Algorithm', 'Fontsize', fontsize);

hold on

% Plot Outerboundary Circular Plane

x0 = 0;

y0 = 0;

radii_plane = 80;

radii_vein = 4;

center_plane = [x0, y0]; % center point of circular plane

viscircles(center_plane, radii_plane, 'color', 'b');

hold on

%%

% plots center of the plane

plot(x0,y0, '*', 'color', 'k', 'markersize', 12)

viscircles(center_plane, radii_vein, 'color', 'k');

hold on

% Create Set of Randoom Points to be Plotted inside circular plane

A = zeros(10,2);

%t = 2*pi*rand(n,1);

%r = r*sqrt(rand(n,1));

%x = r.*cos(t); % x coordinates

%y = r.*sin(t); % y coordinates

t = DataTable.data(:, 1);

r = DataTable.data(:, 2);

x = DataTable.data(:, 3);

y = DataTable.data(:, 4);

A = [x, y]; % stores random points generated

% Plots random points

CTR_graph1 = plot(x, y, '.', 'color', 'r', 'Markersize', 12);

axis square;

% plots circles centered around the randomly generated points

CTR_circles1 = gobjects(n,1);

for i = 1:n

radii_node = 4;

centers_node1 = [x(i), y(i)];

CTR_circles1(i) = viscircles(centers_node1, radii_node, 'color', 'r');

hold on

end

% I use this to draw exclusion range circles

% draw exlcusion range circles b/w hormone seeds and hormone seeds

HS_kept_x = HS_kept(:, 1);

HS_kept_y = HS_kept(:, 2);

LengthHS_kept = length(HS_kept);

radii_node2 = 8;

hold on

for i = 1:n

centers_node5 = [x(i), y(i)];

elim_circles3(i) = viscircles(centers_node5, radii_node2, 'color', 'k', 'linestyle', '--');

end

for j = 1:LengthHS_kept

centers_HS_kept = [HS_kept_x(j), HS_kept_y(j)];

elim_circles4(j) = viscircles(centers_HS_kept, radii_node2, 'color', 'k', 'linestyle', '--');

end

% I use this for HS elimination tests

elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation

HS_HS_threshold = 16;

for i = n:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist2(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);

elim_dist2(j,i) = elim_dist2(i,j);

end

end

% find the points that have its nearest neighbour further away than HS_HS_threshold:

i2keep = find(nanmin(elim_dist2)>HS_HS_threshold);

% put those into one pair of arrays

keep_x = x(i2keep);

keep_y = y(i2keep);

% and the others into another pair of arrays

x_close_neighbours = x;

y_close_neighbours = y;

x_close_neighbours(i2keep) = [];

y_close_neighbours(i2keep) = [];

% EDIT HERE IS THE COMPLETE CODE THAT LEADS TO WHERE IM HAVING THE ISSUES

% FEEL FREE TO MAKE OR SUGGEST ANY CHANGES OR IMPOREMENTS

B = [keep_x, keep_y];

HS_kept = B;

%%

% remove elim circles

delete(elim_circles1);

%%

% delete the old graphs

delete(CTR_graph1);

delete(CTR_circles1);

%%

% Plot Circles around points in new ctr graph

CTR_graph2 = plot(keep_x, keep_y, '.', 'color', 'r', 'markersize', 12);

LengthB = length(B);

for i = 1:LengthB

radii_node = 4;

centers_node2 = [keep_x(i), keep_y(i)];

CTR_circles2 = viscircles(centers_node2, radii_node, 'color', 'r');

hold on

end

%%

% Draw lines between center & points in order of min to max dis.

for i = 1:LengthB

x2(i) = B(i,1);

y2(i) = B(i,2);

Line_matrix1(i,:) = line([x0, x2(i)],[y0, y2(i)], 'linewidth', 1.5, 'color', 'r');

drawnow;

hold on

end

%%

% remove line matrix

pause(q);

delete(Line_matrix1);

%%

% Normal Vector Calculation Storage and Addition

for i = 1:LengthB

C(i,:) = [(x2(i) - x0), (y2(i) - y0)]; % calculates & stores point vectors

mag_vectors(i,:) = norm(C(i,:)); % calculates the magnitude of vectors

end

% Calculates & stores the normalized vectors

for i = 1:LengthB

norm_vectors(i,:) = C(i,:)./mag_vectors(i,:);

end

%%

% Display the normalized vectors

for i = 1:LengthB

scaled_norms = 10.*norm_vectors;

x3(i) = scaled_norms(i,1);

y3(i) = scaled_norms(i,2);

Line_matrix2(i,:) = line([x0, x3(i)],[y0, y3(i)], 'linewidth', 1.5, 'color', 'k');

drawnow;

hold on

end

pause(q);

delete(Line_matrix2);

%delete(arrows);

%%

% Addition Normalized Vectors

x4 = norm_vectors(:,1);

y4 = norm_vectors(:,2);

x5 = norm_vectors(1,1);

y5 = norm_vectors(1,2);

for i = 2:LengthB

x5 = ((x5 - x0) + (x4(i,1)- x0));

y5 = ((y5 - y0) + (y4(i,1) - y0));

end

%%

% Normalize the Combined Vector Addition

D = [x5, y5];

norm_combined = D./norm(D);

%%

% Plot new node at location and in the direction of combined normal vector

vn_prime = 8.*norm_combined;

x5 = vn_prime(:,1);

y5 = vn_prime(:,2);

plot(x5, y5, '*', 'color', 'k', 'markersize', 12);

viscircles(vn_prime, radii_vein, 'color', 'k');

hold on

%%

% Creates array for storing all the vein nodes

vein_node_array = [center_plane; vn_prime;];

vn_x = vein_node_array(:, 1);

vn_y = vein_node_array(:, 2);

%%

% draw exlcusion range circles b/w vein nodes and hormone seeds

for i = 1:LengthB

radii_node2 = 8;

centers_node4 = [keep_x(i), keep_y(i)];

elim_circles2(i,:) = viscircles(centers_node4, radii_node2, 'color', 'r');

end

%%

% Circle of influence elimination VN-HS test & isolates hormone seeds that fail condition

elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation

VN_HS_threshold = 16;

for i = LengthB:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-2):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist2(i,j) = sqrt((keep_x(i)-vn_x(j)).^2 + (keep_y(i)-vn_y(j)).^2);

elim_dist2(j,i) = elim_dist2(i,j);

end

end

% find the points that have its nearest neighbour further away than VN_HS_threshold:

i2keepVN_HS = find(min(elim_dist2)> VN_HS_threshold);

% put those into one pair of arrays

keep_x2 = x(i2keepVN_HS);

keep_y2 = y(i2keepVN_HS);

% and the others into another pair of arrays

x_close_neighbours2 = x;

y_close_neighbours2 = y;

x_close_neighbours2(i2keepVN_HS) = [];

y_close_neighbours2(i2keepVN_HS) = [];

E = [keep_x2, keep_y2];

HS_kept = E;

Adam Danz
on 24 Aug 2019

Edited: Adam Danz
on 24 Aug 2019

ahhh... there we go. The code breaks when we get to "HS_kept" because we don't have that variable and it's a critical one to have for troubleshooting the problem.

I already have some feedback but I'll wait until I can help out with the main problem.

Vance Blake
on 25 Aug 2019

Adam Danz
on 25 Aug 2019

The code still breaks at the same line. Have you tried to run the code you shared?

Vance Blake
on 26 Aug 2019

Edited: Vance Blake
on 26 Aug 2019

Hey Adam Sorry for all the problems i think its because i uploaded separate parts that I missed a few sections of code so Im just gonna repost it all here.

close all;

clear;

clc;

cla;

DataTable = importdata('CTRDataTable_HS_gen1.txt');

n = 10; % total number of points

q = 2; % pause time

fontsize = 12;

axis([-100, 100 -100 100]);

% iterations = 3; % total number of loops

% Axis Labels

xlabel('X', 'Fontsize', fontsize);

ylabel('Y', 'Fontsize', fontsize);

title('Constructal Theory Leaf Modeling Algorithm', 'Fontsize', fontsize);

hold on

% Plot Outerboundary Circular Plane

x0 = 0;

y0 = 0;

radii_plane = 80;

radii_vein = 4;

center_plane = [x0, y0]; % center point of circular plane

viscircles(center_plane, radii_plane, 'color', 'b');

hold on

%%

% plots center of the plane

plot(x0,y0, '*', 'color', 'k', 'markersize', 12)

viscircles(center_plane, radii_vein, 'color', 'k');

hold on

%%

% Create Set of Randoom Points to be Plotted inside circular plane

A = zeros(n, 2);

%t = 2*pi*rand(n,1);

%r = r*sqrt(rand(n,1));

%x = r.*cos(t); % x coordinates

%y = r.*sin(t); % y coordinates

t = DataTable.data(:, 1);

r = DataTable.data(:, 2);

x = DataTable.data(:, 3);

y = DataTable.data(:, 4);

A = [x, y]; % stores random points generated

% Plots n random points

CTR_graph1 = plot(x, y, '.', 'color', 'r', 'Markersize', 12);

axis square;

% plots circles centered around the randomly generated points

CTR_circles1 = gobjects(n,1);

for i = 1:n

radii_node = 4;

centers_node1 = [x(i), y(i)];

CTR_circles1(i) = viscircles(centers_node1, radii_node, 'color', 'r');

hold on

end

%%

% Writes data to text file

%CTRDataTable = table(t, r, x, y, 'VariableNames', {'t', 'r', 'x', 'y',});

%writetable(CTRDataTable, 'CTRDataTable.txt');

%%

% Calculate the distances b/w the center point and each n point

for i = 1:n

distances(i,1) = sqrt((x(i)-x0).^2 + (y(i)-y0).^2);

distances(i,2) = i; % assigns generation order number of the n points

end

% sort the distances from min to max

dist_order = sort(distances(:,1), 'ascend');

% creates a matrix of the distances from min to max based on point number

for i = 1:n

indx(i)=find(distances==dist_order(i));

end

distance_indexnum = transpose(indx); % transposes indx matrix

% arranges distances and point generation number into a matrix with the

% corresponding point number attached

pnd = [dist_order, distance_indexnum];

% Creates matrix of orignial points ordered from min to max distance from

% center for drawing lines to connect with center

for i = 1:n

colpnd = pnd(i,2);

x2(i) = x(colpnd, 1);

y2(i) = y(colpnd, 1);

A(i,:) = [x2(i), y2(i)]; % new matrix of points

end

HS_gen1 = A;

%%

% Draw HS-HS elimination circles

for i = 1:n

radii_node2 = 8;

centers_node1 = [x(i), y(i)];

elim_circles1(i,:) = viscircles(centers_node1, radii_node2, 'color', 'k', 'linestyle', '--');

end

%%

% HS-HS Gen 1 Elimination

elim_dist1 = nan(numel(x)); % places nans on the diagonal after distance calculation

HS_HS_threshold = 24;

for i = n:-1:1 % looping from largest index to avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist1(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);

elim_dist1(j,i) = elim_dist1(i,j);

end

end

% find the points that have its nearest neighbour further away than HS_HS_threshold:

i2keepHS_HS = find(min(elim_dist1)> HS_HS_threshold);

% put those into one pair of arrays

keep_x = x(i2keepHS_HS);

keep_y = y(i2keepHS_HS);

% and the others into another pair of arrays

x_close_neighbours = x;

y_close_neighbours = y;

x_close_neighbours(i2keepHS_HS) = [];

y_close_neighbours(i2keepHS_HS) = [];

B = [keep_x, keep_y];

HS_kept = B;

%%

% remove elim circles

delete(elim_circles1);

%%

% delete the old graphs

delete(CTR_graph1);

delete(CTR_circles1);

%%

% Plot Circles around points in new ctr graph

CTR_graph2 = plot(keep_x, keep_y, '.', 'color', 'r', 'markersize', 12);

LengthB = length(B);

for i = 1:LengthB

radii_node = 4;

centers_node2 = [keep_x(i), keep_y(i)];

CTR_circles2 = viscircles(centers_node2, radii_node, 'color', 'r');

hold on

end

%%

% Draw lines between center & points in order of min to max dis.

for i = 1:LengthB

x2(i) = B(i,1);

y2(i) = B(i,2);

Line_matrix1(i,:) = line([x0, x2(i)],[y0, y2(i)], 'linewidth', 1.5, 'color', 'r');

drawnow;

hold on

end

%%

% remove line matrix

pause(q);

delete(Line_matrix1);

%%

% Normal Vector Calculation Storage and Addition

%Currently this section only works with placing one vein node but I need it to be capable of placing multiple vein nodes at once

% Normal vector addition needs to be capable of adding normal vectors for multiple vein nodes

for i = 1:LengthB

C(i,:) = [(x2(i) - x0), (y2(i) - y0)]; % calculates & stores point vectors

mag_vectors(i,:) = norm(C(i,:)); % calculates the magnitude of vectors

end

% Calculates & stores the normalized vectors

for i = 1:LengthB

norm_vectors(i,:) = C(i,:)./mag_vectors(i,:);

end

%%

% Display the normalized vectors

for i = 1:LengthB

scaled_norms = 10.*norm_vectors;

x3(i) = scaled_norms(i,1);

y3(i) = scaled_norms(i,2);

Line_matrix2(i,:) = line([x0, x3(i)],[y0, y3(i)], 'linewidth', 1.5, 'color', 'k');

drawnow;

hold on

end

pause(q);

delete(Line_matrix2);

%delete(arrows);

%%

% Addition Normalized Vectors

x4 = norm_vectors(:,1);

y4 = norm_vectors(:,2);

x5 = norm_vectors(1,1);

y5 = norm_vectors(1,2);

for i = 2:LengthB

x5 = ((x5 - x0) + (x4(i,1)- x0));

y5 = ((y5 - y0) + (y4(i,1) - y0));

end

%%

% Normalize the Combined Vector Addition

D = [x5, y5];

norm_combined = D./norm(D);

%%

% Plot new node at location and in the direction of combined normal vector

vn_prime = 8.*norm_combined;

x5 = vn_prime(:,1);

y5 = vn_prime(:,2);

plot(x5, y5, '*', 'color', 'k', 'markersize', 12);

viscircles(vn_prime, radii_vein, 'color', 'k');

hold on

%%

% Creates array for storing all the vein nodes

vein_node_array = [center_plane; vn_prime;];

vn_x = vein_node_array(:, 1);

vn_y = vein_node_array(:, 2);

%%

% draw exlcusion range circles b/w vein nodes and hormone seeds

for i = 1:LengthB

radii_node2 = 8;

centers_node4 = [keep_x(i), keep_y(i)];

elim_circles2(i,:) = viscircles(centers_node4, radii_node2, 'color', 'r');

end

%%

% Circle of influence elimination VN-HS test & isolates hormone seeds that fail condition

elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation

VN_HS_threshold = 16;

for i = LengthB:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-2):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist2(i,j) = sqrt((keep_x(i)-vn_x(j)).^2 + (keep_y(i)-vn_y(j)).^2);

elim_dist2(j,i) = elim_dist2(i,j);

end

end

% find the points that have its nearest neighbour further away than VN_HS_threshold:

i2keepVN_HS = find(min(elim_dist2)> VN_HS_threshold);

% put those into one pair of arrays

keep_x2 = x(i2keepVN_HS);

keep_y2 = y(i2keepVN_HS);

% and the others into another pair of arrays

x_close_neighbours2 = x;

y_close_neighbours2 = y;

x_close_neighbours2(i2keepVN_HS) = [];

y_close_neighbours2(i2keepVN_HS) = [];

E = [keep_x2, keep_y2];

HS_kept = E;

%%

% Delete elimination circles

pause(q);

delete(elim_circles2);

%%

% Hormone Seeds Gen 2 Looping Iterations will hopefully start here

% Create 2nd Set of Randoom Points to be Plotted

DataTable2 = importdata('CTRDataTable_HS_gen2.txt');

n = 5; % number of points plotted

%r = 60; % radius of circle

A = zeros(n,2); % Preallocation for A

t = DataTable2.data(:, 1);

r = DataTable2.data(:, 2);

x = DataTable2.data(:, 3);

y = DataTable2.data(:, 4);

% t = 2*pi*rand(n,1);

% r = r*sqrt(rand(n,1));

% x = r.*cos(t); % x coordinates

% y = r.*sin(t); % y coordinates

A = [x, y]; % stores random points generated

% Plots n random points

CTR_graph3 = plot(x, y, '.', 'color', 'g', 'Markersize', 12);

axis square;

CTR_circles3 = gobjects(n, 1);

% plots circles centered around the randomly generated points

for i = 1:n

radii_node = 4;

centers_node5 = [x(i), y(i)];

CTR_circles3(i) = viscircles(centers_node5, radii_node, 'color', 'g');

hold on

end

HS_gen2 = A;

%%

% HERE IS WHERE MY ORIGINAL PROBLEM BEGINS

% Writes HS_gen2 data to text file

%CTRDataTable_HS_gen2 = table(t, r, x, y, 'VariableNames', {'t', 'r', 'x', 'y',});

%writetable(CTRDataTable_HS_gen2, 'CTRDataTable_HS_gen2.txt');

%% HS_HS Elimination Test Gen 2

% draw exlcusion range circles b/w hormone seeds and hormone seeds

HS_kept_x = HS_kept(:, 1);

HS_kept_y = HS_kept(:, 2);

LengthHS_kept = length(HS_kept);

radii_node2 = 8;

hold on

for i = 1:n

centers_node5 = [x(i), y(i)];

elim_circles3(i) = viscircles(centers_node5, radii_node2, 'color', 'k', 'linestyle', '--');

end

for j = 1:LengthHS_kept

centers_HS_kept = [HS_kept_x(j), HS_kept_y(j)];

elim_circles4(j) = viscircles(centers_HS_kept, radii_node2, 'color', 'k', 'linestyle', '--');

end

%%

% Circle of influence elimination HS-HS test & isolates hormone seeds that fail condition

elim_dist3 = nan(numel(x)); % this to have nans on the diagonal after distance calculation

HS_HS_threshold = 24;

for i = LengthHS_kept:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = n:-1:1 % distance symmetric (l(i1,i2) == l(i2,i1))

elim_dist3(i,j) = sqrt((HS_kept_x(i)-x(j)).^2 + (HS_kept_y(i)-y(j)).^2);

elim_dist3(j,i) = elim_dist3(i,j);

end

end

% find the points that have its nearest neighbour further away than HS_HS_threshold:

i2keepHS_HS = find(min(elim_dist3)> HS_HS_threshold);

% put those into one pair of arrays

keep_x3 = x(i2keepHS_HS);

keep_y3 = y(i2keepHS_HS);

% and the others into another pair of arrays

x_close_neighbours3 = x;

y_close_neighbours3 = y;

x_close_neighbours3(i2keepHS_HS) = [];

y_close_neighbours3(i2keepHS_HS) = [];

F = [keep_x3, keep_y3];

HS_kept = [ ;F];

%%

% Delete HS-HS gen 2 elim circles

delete(CTR_circles3);

delete(elim_circles3);

delete(elim_circles4);

%%

% Draw lines to show illustration of distance calculation

for i = 1:LengthB

x2(i) = B(i,1);

y2(i) = B(i,2);

Line_matrix1(i,:) = line([x0, x2(i)],[y0, y2(i)], 'linewidth', 1.5, 'color', 'r');

drawnow;

hold on

end

%%

% remove line matrix

pause(q);

delete(Line_matrix1);

%%

% Normal Vector Calculation Storage and Addition

for i = 1:LengthB

B(i,:) = [(x2(i) - x0), (y2(i) - y0)]; % calculates & stores point vectors

mag_vectors(i,:) = norm(B(i,:)); % calculates the magnitude of vectors

end

% Calculates & stores the normalized vectors

for i = 1:LengthB

norm_vectors(i,:) = B(i,:)./mag_vectors(i,:);

end

%%

% Display the normalized vectors

for i = 1:LengthB

scaled_norms = 10.*norm_vectors;

x3(i) = scaled_norms(i,1);

y3(i) = scaled_norms(i,2);

Line_matrix2(i,:) = line([x0, x3(i)],[y0, y3(i)], 'linewidth', 1.5, 'color', 'k');

drawnow;

hold on

end

% Delete Scaled normal vectors

pause(q);

delete(Line_matrix2);

%delete(arrows);

%%

% Addition Normalized Vectors

x4 = norm_vectors(:,1);

y4 = norm_vectors(:,2);

x5 = norm_vectors(1,1);

y5 = norm_vectors(1,2);

for i = 2:LengthB

x5 = ((x5 - x0) + (x4(i,1)- x0));

y5 = ((y5 - y0) + (y4(i,1) - y0));

end

%%

% Normalize the Combined Vector Addition

C = [x5, y5];

norm_combined = C./norm(C);

%%

% Plot new node at location and in the direction of combined normal vector

vn_prime = 8.*norm_combined;

x5 = vn_prime(:,1);

y5 = vn_prime(:,2);

plot(x5, y5, '*', 'color', 'k', 'markersize', 12);

viscircles(vn_prime, radii_vein, 'color', 'k');

hold on

Adam Danz
on 26 Aug 2019

Edited: Adam Danz
on 26 Aug 2019

Ok, let's solve this together. That way you get to learn how to do some of the troubleshooting.

The code above runs without error. I understand that some of the circles are not plotting in the expected location. Which line(s) of your code is the first line that produces unexpected results? You can just copy-paste that line(s) so I can start looking around that area for the problem.

Vance Blake
on 26 Aug 2019

alright this is where is get my first error with the circles not plotting in the correct spot. I feel it has something to do with the variable reassignment but none of my old deleted points were at those coordinates

HS_kept_x = HS_kept(:, 1);

HS_kept_y = HS_kept(:, 2);

LengthHS_kept = length(HS_kept);

radii_node2 = 8;

hold on

for i = 1:n

centers_node5 = [x(i), y(i)];

elim_circles3(i) = viscircles(centers_node5, radii_node2, 'color', 'k', 'linestyle', '--');

end

for j = 1:LengthHS_kept

centers_HS_kept = [HS_kept_x(j), HS_kept_y(j)];

elim_circles4(j) = viscircles(centers_HS_kept, radii_node2, 'color', 'k', 'linestyle', '--');

end

Adam Danz
on 26 Aug 2019

Edited: Adam Danz
on 26 Aug 2019

Ok, I have feedback for the first half of the code.

One reason this problem is a little bit difficult to trace is because the variable names are being changed, more than once. "HS_kept_x" is a reorganization of "HS_kept" which is a reorganization of "E" which is a reorganization of "keep_x2" which is just a subsample of "x" In order to track how the values are changing throughout the program, we must keep all 5 variables in mind since they are different organizations of the same data. It is much cleaner to just index "x" throughout rather than reassign subgroups of x to multiple variable names. Sometimes it's necessary to assign a new variable name to a subsample of data but doing that 5 times to the same data is unecessary complexity.

This section is unnecessary. You're just producing the 2nd output of sort().

% creates a matrix of the distances from min to max based on point number

for i = 1:n

indx(i)=find(distances==dist_order(i));

end

distance_indexnum = transpose(indx); % transposes indx matrix

% Instead, do this

[dist_order, distance_indexnum] = sort(distances(:,1), 'ascend');

% instead of

pnd = [dist_order, distance_indexnum];

% you could do

pnd = distances(distance_indexnum,:)

% Instead of

for i = 1:n

colpnd = pnd(i,2);

x2(i) = x(colpnd, 1);

y2(i) = y(colpnd, 1);

A(i,:) = [x2(i), y2(i)]; % new matrix of points

end

% Do

A = [x(pnd(:,2)), y(pnd(:,2))];

HS_gen1 = A; % Why change the variable name? This makes

% the code very difficult to read.

Now we get to this section which I don't understand. I'm fairly certain that this is where the error starts to occur. Could you explain what elim_dist1 is? For example, what are the columns and rows? How does elim_dist1(i,j) relate to x and y?

% HS-HS Gen 1 Elimination

elim_dist1 = nan(numel(x)); % places nans on the diagonal after distance calculation

HS_HS_threshold = 24;

for i = n:-1:1 % looping from largest index to avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist1(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);

elim_dist1(j,i) = elim_dist1(i,j);

end

end

Vance Blake
on 26 Aug 2019

Edited: Vance Blake
on 26 Aug 2019

1. ok so the reason I change the create the variable HS_gen1 = A is just to keep a record of the first set of points im working with. I need to keep well document records of each step i go thru so that I can explain it to anyone who is looking at the code/accompanying documents and reference how each step of the plot unfolds. As you can see from how the code is sectioned into pieces I am running each piece step wise and taking screengrabs of how the plot develops. I don't use HS_gen1 for any calculaation steps. I just want to save each generation of new hormone seeds (HS) for each iteration of the code in the future when the program is hopefully capable of running continously until end condition of all the points in HS_kept ends up being eliminated by HS_VN threshold that is no more vein nodes (VN) can be placed because they absorb all the existing HS. What ive realized is that I need to keep a temporary matrix of all the currently existing HS that is separate from the HS that survive all the elimination criteria.

2. For the changes you are suggesting this is all vecotrization right?? What are the mechanics of the code like when you with the other circles problem I got that we needed to tie each cicrle to the loop with 'i' but how does vectorization bypass that need?? I trying to train myself to think in terms of vectorization instead of for loops which are slower and require preallocation.

3. elim_dist is a symmetric matrix of the distance values between each hormone seeds HS (red dots). It compares the xy coordinates of each point on the plot. I use it to sort and remove points that are too close together i.e. less than the HS_HS threshold of 24. I use the same mechanism for eliminating HS that are too close to the vein nodes using VN_HS_threshold of 16. What i want to do is eliminate the points the fail these criteria and then conduct the vector calculations for placing new vein nodes. I asked another question that contains illustrations of what im trying to do. Below is a link to the other question.

Adam Danz
on 26 Aug 2019

#1: ok, makes sense.

#2: it's vectorization combined with indexing. Here's a good resource: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html

3: Ok, got it.

First, this block of code....

% HS-HS Gen 1 Elimination

elim_dist1 = nan(numel(x)); % places nans on the diagonal after distance calculation

HS_HS_threshold = 24;

for i = n:-1:1 % looping from largest index to avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist1(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);

elim_dist1(j,i) = elim_dist1(i,j);

end

end

...can be replaced with these 2 lines.

elim_dist1 = squareform(pdist([x,y]));

elim_dist1(1:size(elim_dist1,1)+1:end) = NaN;

Now let's look at this similar block of code. This is where the error is happening.

% Circle of influence elimination VN-HS test & isolates hormone seeds that fail condition

elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation

VN_HS_threshold = 16;

for i = LengthB:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-2):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist2(i,j) = sqrt((keep_x(i)-vn_x(j)).^2 + (keep_y(i)-vn_y(j)).^2);

elim_dist2(j,i) = elim_dist2(i,j);

end

end

% find the points that have its nearest neighbour further away than VN_HS_threshold:

i2keepVN_HS = find(min(elim_dist2)> VN_HS_threshold);

% put those into one pair of arrays

keep_x2 = x(i2keepVN_HS);

keep_y2 = y(i2keepVN_HS);

Have you looked at the values of elim_dist2? Here they are below, after both loops are complete.

elim_dist2 =

NaN NaN 57.516 58.335 NaN NaN NaN NaN NaN NaN

NaN NaN NaN 50.37 NaN NaN NaN NaN NaN NaN

57.516 NaN NaN NaN NaN NaN NaN NaN NaN NaN

58.335 50.37 NaN NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

The i-loop only loops over 4 iterations because "LenghB" (bad variable name) equals 4. So you'll never get past the 4 row or column of elim_dist2 even though there are 10 rows and columns. The rest will always be NaNs.

Then you find which columns in elim_dist2 have values greater than 16 and of course that will never include columns 5:10 because they are all NaNs. Nevertheless, you use this index to select values of x and y which have 10 elements.

That makes no sense. So you probably want to re-think this section and what it's supposed to be doing.

Vance Blake
on 27 Aug 2019

Adam Danz
on 27 Aug 2019

Edited: Adam Danz
on 27 Aug 2019

"I only want it to keep the 4 HS points at that stage of the code. "

i2keepVN_HS is equal to [1,2,3,4] because columns 5:10 of elim_dist2 are always NaNs.

x and y are vectors and each of them have 10 elements. So, the two lines below will only choose the first 4 element of x and y and will always ignore the rest as if they don't even exist.

Is that really want you want to do, to always ignore elements 5 to 10 of x and y?

keep_x2 = x(i2keepVN_HS);

keep_y2 = y(i2keepVN_HS);

"keep_x2" becomes part of "E" which is renamed to "HS_kept" which is part of "HS_kept_x" (a nightmare of variable name changes) and HS_kept_x (which is the same as keep_x2) are the values that are creating circles in the unexpected areas. So I'm quite certain that's the source of your error. I think you should run the code up to the beginning of that section and step through each of those line, line-by-line, and think about what each variable is supposed to represent, what it's supposed to look like, the size, shape, and the values, in order to find the glitch.

Vance Blake
on 27 Aug 2019

ok I see what you mean I don't want it to ignore the rest of the elements in 5:10 for any step. I thought that it was just picking up on the NaNs. So I need to rewrite the second elimination test so that it is no longer working with the original x and y and instead use the chosen x and y that survived the first HS-HS elimination test. I was reading your comment and I was thinking to myself any (keep_x* keep_y* with * = 1 or 2) should be only a 4 by 1 matrix in the first iteration because there should only be 4 points remaining after the first HS-HS elimination test. So if my understanding is correct the way the code is written now for the second test is going back to the original x and y instead of the surviving x and y coordinates stored in matrix B??

Also I know the variable name changes are horrendous but im not at the step where I want to overwirte the data relating to any steps because I want to be ablle to trace back and show that each step is doing exactly what I wanted which thanks to your help I have understood that is not the case for the second elimination test lol.

Adam Danz
on 27 Aug 2019

"So I need to rewrite the second elimination test so that it is no longer working with the original x and y and instead use the chosen x and y that survived the first HS-HS elimination test."

&

"So if my understanding is correct the way the code is written now for the second test is going back to the original x and y instead of the surviving x and y coordinates stored in matrix B"

That sounds right! You know the code better than I and that sounds logical.

Vance Blake
on 27 Aug 2019

alright so i tried reworking the code but for some reason it is still treating elim dist 2 as 10x10 i think it has something to do with this elim_dist2(j,i) = elim_dist2(i,j); Here is how i rewrote the code. Would it be better off just replacing the for loops with the code you suggested earlier ??

elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation

VN_HS_threshold = 16;

for i = SB:-1:1 % looping from largest index lets us avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix

for j = (i-2):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once

elim_dist2(i,j) = sqrt((keep_x(i)-vn_x(j)).^2 + (keep_y(i)-vn_y(j)).^2);

elim_dist2(j,i) = elim_dist2(i,j);

end

end

% find the points that have its nearest neighbour further away than VN_HS_threshold:

i2keepVN_HS = find(min(elim_dist2)> VN_HS_threshold);

% put those into one pair of arrays

keep_x2 = keep_x(i2keepVN_HS);

keep_y2 = keep_y(i2keepVN_HS);

% and the others into another pair of arrays

x_close_neighbours2 = keep_x;

y_close_neighbours2 = keep_y;

x_close_neighbours2(i2keepVN_HS) = [];

y_close_neighbours2(i2keepVN_HS) = [];

E = [keep_x2, keep_y2];

HS_kept = E;

Vance Blake
on 28 Aug 2019

Edited: Vance Blake
on 28 Aug 2019

Adam Danz
on 28 Aug 2019

### Accepted Answer

Adam Danz
on 28 Aug 2019

Summary of the discussion in the comment section under the question:

A matrix was pre allocated incorrectly which led to indexing problems.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)