Connect 3D Scatter points through a line

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

To check: if you had (say) 7 points, then you would want each point to have a line to each of the other 6 points? A "complete graph" in graph theory terms?
Or are you wanting to connect to points that are "adjacent" ?
I want them to connect to the points that are adjacent to one another in 3d.
Okay, so now: given lists of x, y, z, in which the entries are not ordered relative to each other but x(K) corresponds to y(K) and z(K), then: how do you determine whether [x(J), y(J), z(J)] is adjacent to [x(K), y(K), z(K)] ? Is there a cut-off distance? k nearest neighbours ?
There is no cut-off distance. I would like it to connect every point to the points around it, if that makes more sense to you.
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?
I think you are correct. How would I connect every point to every other point?
Thank you,

Sign in to comment.

Answers (1)

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

Hey, how do you only connect the nearest points?
x=[EEG.chanlocs.X];
y=[EEG.chanlocs.Y];
z=[EEG.chanlocs.Z];
(x,y,z are 64 channels with their respective location (1x64))
[xi, yi ] = meshgrid(x,y);
F = scatteredInterpolant(x',y',z','natural','none');
zi=F(xi,yi);
rotate3d on
hold on;
plot3(x, y, z,'r.')
cut-off distance would be okay, but would need to be defined. Points are relatively evenly spaced (head-shaped)
@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

Asked:

on 26 Nov 2018

Edited:

on 31 Mar 2021

Community Treasure Hunt

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

Start Hunting!