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
Best Answer