Sorting a vector with another vector of multiple maximums

1 visualizzazione (ultimi 30 giorni)
Hi,
I have a function that I want to compute a queue "Qevents", which gets information from a set of points "P" (columns x and y) and sorts them by maximum y value. If there are other (same) maximum y values, I want to store the maximum y-value that has the largest corresponding x value. As the code is executed, points are deleted from the original data set so a new maximum can be examined. I have started writing out the code to this:
% given a list of points P(n,2) representing x,y coordinates
for i=1:length(P)
indx = P==max(P(:,2)); % find index of maximum in set of points..
if "multiple maximums" % don't know the code for this line
if indx(i,1)==1 && indx(i,2)==1 % checks for maximums in both columns
Qevents(i) = max(P(:,2)); % stores maximum y-value recursively
P(i,:) = []; % and removes it from the list of points
end
else
Qevents(i) = max(P(:,2))
P(indx) = [];
end
end
I have no idea how to check for multiple maximums and use it in a loop properly. Any help would be appreciated. Thanks!
Ian

Risposta accettata

kjetil87
kjetil87 il 14 Ago 2013
Modificato: kjetil87 il 14 Ago 2013
Im not a 100% sure if this is what you are asking for, but as for checking for multiple maximums you are actually already doing this.
Example:
%here test(:,2) has two max values.
test=[1,2;...
2,3;...
3,3];
indx=test(:,2)==max(test(:,2))
indx =
0
1
1
index now holds true if test(:,2) is the maximum value. if there are more then 1 true element in indx , it means that there are several maxima so to test for this simply use :
if nnz(indx)>1
to check for the maximum x value (i assume this is test(:,1) ) you do the same exercise but use logical indexing to sort out the possible candidates:
indxX=test(:,1)==max(test(indx,1));%i assume that x values cannot be equal
indxX now holds the index for the row containing maximum value of both x and y.
so Qevents(i)=test(indxX,2);
Another more readable solution would be to use sort and find.
test=[1,2;...
3,3;...
2,3]; % i now made sure that the x values are not sorted.
% sort the array with respect to x values.
[~,idx]=sort(test(:,1));
test=test(idx,1:2); % remember to also switch around the y values.
Then you can use find(..) to get the last maximum y value (since x is now sorted)
indx=find(test(:,2)==max(test(:,2)),1,'last');
Note that index is now a scalar index and not a logical vector. But still the value you are looking for is:
test(indx,2);
Play around with the two methods see which one fits you best =)
  1 Commento
Ian Wood
Ian Wood il 14 Ago 2013
Thanks for your help! This is perfect and exactly what I needed. Sort is a very useful tool to do deal with multiple maximum values.

Accedi per commentare.

Più risposte (1)

Ian Wood
Ian Wood il 14 Ago 2013
Modificato: Ian Wood il 15 Ago 2013
With the help of kjetil87, I was able to write the appropriate code for this situation. It is shown here:
[~,idx] = sort(P(:,1)); % sort x values in increasing order
P = P(idx,1:2); % sort y values corresponding to increasing x values
for i=1:length(P)
indx = find(P(:,2)==max(P(:,2)),1,'last'); % find index with maximum y
Qevents(i,:) = P(indx,:); % store maximum y-value recursively
P(indx,:) = []; % and remove it from the list of points
end
Now the Qevents vector is properly stored with y-values in decreasing order corresponding to their x-value!
Ian
Edit: improved the code to not include unnecessary actions inside of the loop.

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by