필터 지우기
필터 지우기

How to find common centroids?

조회 수: 4 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2015년 7월 8일
답변: Guillaume 2015년 7월 8일
Given: I have coordinates of 5 center points. For example:
centroids =
[] [156,140]
[114,167][156,141]
[117,172][157,142]
[155,140][]
[153,141][]
Want: I want to find nearest common centroid. Such as:
Common_centroids =
[156,140]
[156,141]
[157,142]
[155,140]
[153,141]
Currently done:
% remove blank cells matlab
centroids = centroids_test2'; centroids(cellfun(@isempty,centroids)) = [];
% convert the contents of a cell array into a single matrix.
centroid =[cell2mat(centroids')];
% find mean value
mean_centroid = round(mean(centroid));
% x coordinates of cenroid
centroid_x = centroid(:,1);
% y coordinates of cenroid
centroid_y = centroid(:,2);
%find x values more than mean
index1 = find(repmat(mean_centroid(1),length(centroid),1) < centroid_x);
% find y values less than mean
index2 = find(repmat(mean_centroid(2),length(centroid),1) > centroid_y);
Common_centroids = [centroid_x(index1),centroid_y(index2)];
Here is the output:
Common_centroids =
156 140
156 141
157 142
155 140
153 141
As you can see code itself is not robust at all, because I have to specify which mean value to find.
Needed: So I'm wondering, if there are any other better and more efficient way of doing it?
[ACKNOWLEDGMENTS]
Thanks in advance for any help.
I will vote for all your answers.
[MATLAB version]
R2014a

채택된 답변

Guillaume
Guillaume 2015년 7월 8일
This is one way of doing it:
centroids = {[], [156 140]; [114 167], [156 141]; [117 172], [157 142]; [155 140], []; [153 141], []};
cmedian = median(vertcat(centroids{:}));
%convert centroids with a 3d array with [inf inf] for missing centroids
infcentroids = centroids;
infcentroids(cellfun(@isempty, centroids)) = {[inf inf]};
infcentroids = reshape(cell2mat(infcentroids), size(infcentroids, 1), 2, []);
%calculate square of euclidean distance from median:
edist = sum(bsxfun(@minus, infcentroids, cmedian).^2, 2);
%find minimum in each row:
[~, closest] = min(edist, [], 3);
%use that to index centroids:
common_centroids = cell2mat(centroids(sub2ind(size(centroids), (1:size(centroids, 1))', closest)))

추가 답변 (1개)

Thorsten
Thorsten 2015년 7월 8일
I is not exactly clear to me what you want to achieve. To order C depending on distance to the mean:
C{1} = [];
C{2} = [156,140];
C{3} = [114,167];
C{4} = [156,141];
C{5} = [117,172];
C{6} = [157,142];
C{7} = [155,140];
C{8} = [];
C{9} = [153,141];
C = reshape(cell2mat(C), 2, [])';
[distCmean ind] = sort(sum((C - repmat(mean(C), [size(C,1) 1])).^2, 2));
C(ind)
  댓글 수: 1
Ivan Shorokhov
Ivan Shorokhov 2015년 7월 8일
편집: Ivan Shorokhov 2015년 7월 8일
@Thorsten, I want to find median value (of center points) and then according to that median value find nearest coordinates in the array.
Something like this:
median_x = median(sort(centroid_x));
median_y = median(sort(centroid_y));
[row1, col2] = find(centroid_x>median_x*0.9 & centroid_x<median_x*1.1) % find x values more than mean
[row3, col4] = find(centroid_y>median_y*0.9 & centroid_y<median_y*1.1) % find x values more than mean
Common_centroids2 = [centroid_x(row1),centroid_y(row3)]

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by