assigning points to clusters using index array

I'm trying to cluster a point cloud (nx2 array with x and y coordinate) based on the distance of the point to a line. For example:
points = [1 5; 2 6; 4 8]
points = 3×2
1 5 2 6 4 8
Using a for loop i have already detemined what line each point is closest to. The for loop puts an index in an nx1 array that represents the cluster/line that point belongs to. The amount of lines is variable. With two possible lines this array for the above points might look like this:
clusterIndex = [1; 2; 1]
clusterIndex = 3×1
1 2 1
Using these two arrays I want to cluster the points with x and y coordinates seperatly. Where the first column represents the first cluster and so on. Resulting in this:
x = [1 2; 4 0]
x = 2×2
1 2 4 0
y = [5 6; 8 0]
y = 2×2
5 6 8 0
Using the list of indices I can't get the points sorted in clusters. If there is a better way instead of using the indices that would also be apreciated.

回答 (2 件)

Max Alger-Meyer
Max Alger-Meyer 2022 年 3 月 7 日

1 投票

So here is what you asked for using the nnz function:
%given quantities
points = [1 5; 2 6; 4 8];
clusterIndex = [1; 2; 1];
x = zeros(1,max(clusterIndex));
y = zeros(1,max(clusterIndex));
%Outputs:
for i = 1:size(points,1)
x(nnz(x(clusterIndex(i)))+1,clusterIndex(i)) = points(i,1);
y(nnz(y(clusterIndex(i)))+1,clusterIndex(i)) = points(i,2);
end
%Here are the results:
x
x = 2×2
1 2 4 0
y
y = 2×2
5 6 8 0
This method uses the nnz() function which identifies how many nonzero elements there are in an array and adds 1 to that number to determine which row a given x or y coordinate should be placed in.

3 件のコメント

Tibo Huyers
Tibo Huyers 2022 年 3 月 7 日
I have implemented your code in my matlab script were i currently have 136 points and two clusters. The output still is a 2x2 for x and y. I think i understand what the nnz function is supposed to do in this case. The way you use it means that the input for nnz will always be a number allready inside the array x or y and not an actual array. For example when i = 2, clusterIndex(2) will be either 1 of 2, then x(1 or 2) will just be the element inside x, right?
Tibo Huyers
Tibo Huyers 2022 年 3 月 7 日
編集済み: Tibo Huyers 2022 年 3 月 7 日
I think this fixes the problem:
x(nnz(x(:,clusterIndex(i)))+1,clusterIndex(i)) = points(i,1);
y(nnz(y(:,clusterIndex(i)))+1,clusterIndex(i)) = points(i,2);
The
:,
is needed to give the nnz function the correct column and not just an element.
Max Alger-Meyer
Max Alger-Meyer 2022 年 3 月 7 日
Yep, sorry about that. Forgot to include the colon but yes you are correct because we only want to identify the number of nonzero elements in the corresponding column.

サインインしてコメントする。

Davide Masiello
Davide Masiello 2022 年 3 月 7 日

0 投票

points = [1 5; 2 6; 4 8];
N = size(points,1);
n = N+mod(N,2);
x = zeros(1,n);
y = x;
x(1:N) = points(:,1);
y(1:N) = points(:,2);
x = reshape(x,2,2);
y = reshape(y,2,2);

3 件のコメント

Tibo Huyers
Tibo Huyers 2022 年 3 月 7 日
Thanks for the answer but this doens't use the indices to cluster the points. The way i understand your code is it just reshapes the list of x and y coordinates to achieve something similar. The x and y that your code outputs is not the same as my example, if i transpose the matrices it is the same by chance.
For clarification, the indices might asswell be viewed as a random list of integers between 1 and the amount of lines (2 in my example).
Max Alger-Meyer
Max Alger-Meyer 2022 年 3 月 7 日
I think you missed what is being asked. Your code seems to just take all of the x or y coordinates and arrange them into either a 2 x 2 array, but the column should correspond to the segment that that coordinate is closest to which is designated by the corresponding row of clusterIndex. Your code also looks like it will break for more than 4 points.
Davide Masiello
Davide Masiello 2022 年 3 月 7 日
Sorry, I see the point now.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 3 月 7 日

コメント済み:

2022 年 3 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by