MATLAB Answers

Sorting a data set to report failing values in pairs instead of individually

1 view (last 30 days)
Vance Blake
Vance Blake on 11 Nov 2019
Edited: Vance Blake on 14 Nov 2019
Hello, I am working with a set of 10 xy pairs of points in my data set. I sort the data based on how far apart the 10 points are from each other on my plot. I have a bit of code which allows me to determine which points pass and fail this threshold test, but it reports the numbers individually and I wish to report them as pair based on their index numbers in the matrix A, and the then retain the point in each pair which has the lower index number. For example, if point 1 and point 8 & point 7 and point 10 are too close to each other then, I would like for them to be stored as a pair and then select points 1 and 7 for retention and points 8 and 10 for elimination. I have attached the code I am using to sort them with along witht the data set I am flitering. Thanks for any and all tips.
% CTR Surface Area Simulator
close all;
n = 10; % total number of points
elim_radii = 12;
radii_node = 4;
fontsize = 12;
axis([-100, 100 -100 100]);
% iterations = 3; % total number of loops
% Axis Labels
xlabel('X', 'Fontsize', fontsize);
ylabel('Y', 'Fontsize', fontsize);
title('CTR Surface Area 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
% Hormone Seeds Gen 2 Calculations
% Create 2nd Set of Randoom Points to be Plotted
CTR_HStoSA1076_2 = importdata('CTR_HStoSA1076_gen2.txt');
A = zeros(n, 2); % Preallocation for A
t =, 1);
r =, 2);
x =, 3);
y =, 4);
A = [x, y]; % stores random points generated
% Plots n random points
CTR_graph2 = plot(x, y, '.', 'color', 'g', 'Markersize', 12);
axis square;
CTR_circles2 = gobjects(n, 1);
% plots circles centered around the randomly generated points
for i = 1:n
centers_node2 = [x(i), y(i)];
CTR_circles2(i,:) = viscircles(centers_node2, radii_node, 'color', 'g');
hold on
HS_gen2 = A;
% HS_HS Elimination Test Gen 2
% draw exlcusion range circles b/w hormone seeds and hormone seeds for HS gen 2
elim_circles2 = gobjects(n, 1);
for i = 1:n
centers_node2 = [x(i), y(i)];
elim_circles2(i) = viscircles(centers_node2, elim_radii, 'color', 'k', 'linestyle', '--');
% HS-HS Gen 2 Elimination HS gen 2 vs HS gen 2
elim_dist4 = nan(numel(x)); % creates nan matrix and places nans on the diagonal after distance calculation
HS_HS_threshold = 12;
for i = 1:n
for j = 1:(i-1) % distance symmetric (l(i1,i2) == l(i2,i1))
elim_dist4(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
elim_dist4(j,i) = elim_dist4(i,j);
% find the points that have its nearest neighbor further away than HS_HS_threshold:
i2keepHS_HS2 = find(min(elim_dist4)> HS_HS_threshold);
i2notkeepHS_HS2 = find(min(elim_dist4)< HS_HS_threshold);
keep_x4 = x(i2keepHS_HS2);
keep_y4 = y(i2keepHS_HS2);
x_close_neighbors4 = x;
y_close_neighbors4 = y;
x_close_neighbors4(i2keepHS_HS2) = [];
y_close_neighbors4(i2keepHS_HS2) = [];
G = [keep_x4, keep_y4];
experiment = [x_close_neighbors4, y_close_neighbors4];


Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 11 Nov 2019
Edited: Adam Danz on 12 Nov 2019
I think you're looking for....
% Coordinates you're keeping
keepCoordinates = A(i2keepHS_HS2, :);
% Coordinates you're not keeping
tossCorrdinates = A(i2notkeepHS_HS2, :);
Idea #2 (added after the initial discussion below).
When you create the distance matrix, you only need the upper or lower triangle of the matrix. I think the problem is solved by...
for i = 1:n
for j = 1:(i-1) % distance symmetric (l(i1,i2) == l(i2,i1))
elim_dist4(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
% elim_dist4(j,i) = elim_dist4(i,j); % REMOVE THIS


Show 2 older comments
Vance Blake
Vance Blake on 13 Nov 2019
yeah thats what I realized and did to get it to work. I used the triu() function though. After that it was just recording the row and col numbers of the distances that were less than the threshold and saving them to a matrix togoether.
Adam Danz
Adam Danz on 13 Nov 2019
If this is what you're doing
for i = 1:n
for j = 1:(i-1) % distance symmetric (l(i1,i2) == l(i2,i1))
elim_dist4(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
elim_dist4(j,i) = elim_dist4(i,j);
% ...
elim_dist4U = triu(elim_dist4);
that's the same as just removing the 2nd line within the for-j loop.
Vance Blake
Vance Blake on 14 Nov 2019
Yeah I tried that at first cause I used that technique before, but I ran into problems when I was trying to report the row and col numbers in the next step of the code. It kept reporting the row values a 1 and only the column values as 1, 7, 8, 10. When what I needed was [1,8; 7, 10];. I dont know why exactly the code was doing that probably some mistake I didnt account for, but it worked once I kept the matrix symmetric and just took the upper or lower triangular matrix to get the row and col numbers together. I can post the final result if you want to see if there are any improvements that can be made. But, I dont remember the lines of code that did not work, so i couldn't really learn what the mistake was.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by