Connect 3D Scatter points through a line

12 views (last 30 days)
JB B
JB B on 26 Nov 2018
Edited: Image Analyst on 31 Mar 2021
So, I have a 3D scatter plot that I would like to connect every point to one another by using line. If someone can guide me to that, I would greatly be appreciated.3dsca
  6 Comments
Walter Roberson
Walter Roberson on 26 Nov 2018
It would make sense to connect every point to every other point. Otherwise there has to be a rule to determine whether a point is "around" another point.
Let us take an example of reasonable size:
rng(1);a = randi(10,10, 3);
scatter3(a(:,1), a(:,2), a(:,3));
Now, which points are to be connected to which?
JB B
JB B on 27 Nov 2018
I think you are correct. How would I connect every point to every other point?
Thank you,

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 27 Nov 2018
This should connect every point to every other point (untested)
hold on;
for k1 =1 : length(x)-1
for k2 = k : length(x)
plot3([x(k1), x(k2)], [y(k1), y(k2)], [z(k1), z(k2)], 'b-');
end
end
If you don't want all points connected to all other points, but only "adjacent" ones, then you have to define "adjacent" or "around". Is it only the nearest other point? Or 5 nearest?
  4 Comments
Lukas
Lukas on 31 Mar 2021
cut-off distance would be okay, but would need to be defined. Points are relatively evenly spaced (head-shaped)
Image Analyst
Image Analyst on 31 Mar 2021
Edited: Image Analyst on 31 Mar 2021
@Lukas Gaßmann Start your own question with your EEG data attached in a .mat file and we'll show you how you can use pdist2() to find close points.
In the meantime, try the code below and if it helps you click on the Vote button near my top answer.
numPoints = 200;
x = 10 * rand(numPoints, 1);
y = 10 * rand(numPoints, 1);
z = 10 * rand(numPoints, 1);
subplot(2, 1, 1);
plot3(x, y, z, 'b.');
grid on;
title('Histogram of Distances');
% Find distance of every point to every other point.
xyz = [x, y, z];
distances = pdist2(xyz, xyz)
subplot(2, 1, 2);
histogram(distances);
grid on;
title('Histogram of Distances');
% Find distances closer than 3 units.
threshold = 1;
[index1, index2] = find(distances <= threshold)
indexes = unique([index1, index2], 'rows')
xline(threshold, 'Color', 'r', 'LineWidth', 2);
% For every pair of close points, draw a line.
subplot(2, 1, 1);
hold on;
for k = 1 : size(indexes, 1)
xPair = [x(indexes(k, 1)), x(indexes(k, 2))];
yPair = [y(indexes(k, 1)), y(indexes(k, 2))];
zPair = [z(indexes(k, 1)), z(indexes(k, 2))];
plot3(xPair, yPair, zPair, 'r-', 'LineWidth', 2);
end
fprintf('Done running %s.m\n', mfilename);

Sign in to comment.

Categories

Find more on Graph and Network Algorithms 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!