How can I only keep keys that have the smallest value pair in a matrix?

1 回表示 (過去 30 日間)
Christian Garner
Christian Garner 2019 年 6 月 18 日
回答済み: Walter Roberson 2019 年 6 月 18 日
I'm trying to create an edge detection program that creates a matrix with coordinates of just the surface of whatever subject we're examining. After using the canny edge detection filter I have all the coordinates of what the filter is calling the surface, which includes multiple rows within the same column. I'm trying to create a new matrix such that only the smallest row number numbers are assigned to each column.
For instance, If I have an n x 2 array like
A = [1 10; 1 15; 1 20; 2 35; 2 46; 2 57;]
I would want an output that looks like
B = [1 10; 2 35]

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 6 月 18 日
[g, val] = findgroups(A(:,1));
B = [val, accumarray(g, A(:,2), [], @min)];

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by