choose two coordinates
1 view (last 30 days)
Show older comments
hi there, i have a set of xy coordinates show in command window.So, I want to pick the coordinate where, there are the most many number repeated in y. here i attach the illustration for reference. see the attached for more information.
help me please.. thanks
0 Comments
Accepted Answer
Matt Tearle
on 19 Apr 2012
idx = (y == mode(y));
x(idx)
y(idx)
or, if x and y are columns of a matrix
idx = (xy(:,2) == mode(xy(:,2)));
xy(idx,:)
EDIT TO ADD:
Note sure if I'm correctly interpreting your follow-up question, but try this (try a few times, because random numbers sometimes do strange things):
xy = randi(10,12,2); % Make some fake data
% Find the coordinates with the most equal y values
idx = (xy(:,2)==mode(xy(:,2)));
mostycoords = xy(idx,:);
% Take first and last coordinates
firstlast = mostycoords([1,end],:);
% Plot results
plot(xy(:,1),xy(:,2),'o',firstlast(:,1),firstlast(:,2))
axis([0,12,0,12])
More Answers (1)
Image Analyst
on 19 Apr 2012
Try this:
% Generate some sample data,
xy = [0 3; 1 2;3 4; 3 2; 2 5; 7 2; 3 9]
modeRowIndexes = xy(:,2) == mode(xy(:,2))
% Now we know the rows that have the mode in the second column.
% So now we can find the first and last row where they occur.
firstRow = find(modeRowIndexes, 1, 'first')
lastRow = find(modeRowIndexes, 1, 'last')
% Get the first occurrence (coordinate) into a 1 by 2 array.
firstCoordinate = [xy(firstRow, 1), xy(firstRow, 2)]
% Get the second occurrence (coordinate) into a 1 by 2 array.
lastCoordinate = [xy(lastRow, 1), xy(lastRow, 2)]
In the command window you'll see the explanation:
xy =
0 3
1 2
3 4
3 2
2 5
7 2
3 9
modeRowIndexes =
0
1
0
1
0
1
0
firstRow =
2
lastRow =
6
firstCoordinate =
1 2
lastCoordinate =
7 2
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!