Clear Filters
Clear Filters

choose two coordinates

2 views (last 30 days)
mohd
mohd on 19 Apr 2012
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

Accepted Answer

Matt Tearle
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])
  1 Comment
mohd
mohd on 19 Apr 2012
thanks a lot sir..:-).you are very good response and understanding.:-). the code really i need. question again.. how to plot using that coordinates. i want to draw straight line using that coordinate on my image.

Sign in to comment.

More Answers (1)

Image Analyst
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
  1 Comment
mohd
mohd on 19 Apr 2012
wow.. it really make me happy and has fun in matlab. thanks a lot sir..;-). i'm so worry if everyone can not understand what i mean. but it is easy to get understand my question. again thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!