Find the highest value of y for each x value

16 vues (au cours des 30 derniers jours)
Yaser Khojah
Yaser Khojah le 27 Avr 2019
Modifié(e) : Yaser Khojah le 29 Avr 2019
Can anyone please help me with this quesiton. I have a big matrix where I’m only interested to plot the highest value of y for each x value. My x-axis ranges between 0 and 45 and I want the highest values for each one. So, I created the following code but I’m not happy with it. I also attached my data. Thank you for your time.
x = MaT_All(:,18);
y = MaT_All(:,17)
edges = 200; % I used a number as 200 since I could not find the indexs of 0:45 in the original data (x)
Group = discretizediscretize(x ,edges); %
Result_y = accumarray(Group, y , [], @max); % I got the highest values (y) but not sure how to find the corresponding value in x
scatter(Result_x ,Result_y) % how can i find Result_x
I also tried this code
[G,TID] = findgroups(x);
Max_NPV = splitapply(@max,y),G);
scatter(TID,Max_NPV);
  1 commentaire
Cedric
Cedric le 27 Avr 2019
What do you mean by "not happy with it"?

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 27 Avr 2019
How about (untested):
ux = unique(x)
hold on;
for k = 1 : length(ux) % For each unique x...
% Find the max y for this x value.
maxY = max(y == ux(k));
% Plot that point.
plot(ux(k), maxY, 'b.', 'MarkerSize', 10);
end
grid on;
  1 commentaire
Yaser Khojah
Yaser Khojah le 29 Avr 2019
Modifié(e) : Yaser Khojah le 29 Avr 2019
thanks fo your help but that does not give me what i'm looking for. I have this code but i still do not know how to find the index of the ymax?
x = round(MaT_All(:,18),0);
y = MaT_All(:,17);
[uv,~,idx] = unique(x);
ymax = accumarray(idx,y,[],@max); %build matrix
[uv ymax];
figure
scatter(uv,ymax)
line(uv,ymax)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by