MATLAB Answers


How does one plot only the maximum y value for an x value with multiple y values?

Geoff Doback さんによって質問されました 2019 年 7 月 8 日
最新アクティビティ Star Strider
さんによって コメントされました 2019 年 7 月 9 日
I have two vectors (y4 and T4) and need to plot y4 on the x-axis and T4 on the y-axis. The only problem is that there exists multiple values of T4 for each value of y4, so how can I only plot the highest of the T4 values for each y4? I have attached my two vectors for reference.

  0 件のコメント

サインイン to comment.

2 件の回答

回答者: Star Strider
2019 年 7 月 9 日

The accumarray function is perfect for this:
D = load('y4-T4.mat');
T4 = D.T4; % Define ‘T4’
y4 = D.y4; % Given ‘y4’
[Uy4,~,Ix] = unique(y4); % Unique Values Of ‘y4’
T4Max = accumarray(Ix, T4, [], @max); % Maximum Values Of ‘T4’
y4T4 = [Uy4, T4Max]; % Matrix OF Unique & Corresponding Maximum Values
plot(Uy4, T4Max)
ylim([min(ylim) 850])
How does one plot only the maximum y value for an x value with multiple y values - 2019 07 08.png
Other options are possible, including the mean, median, standard deviation, and others, just by changing the function call in accumarray.

  2 件のコメント

Worked perfectly - thanks!
Star Strider
2019 年 7 月 9 日
As always, my pleasure!

サインイン to comment.

回答者: Be Matlabi
2019 年 7 月 9 日

Hi Geoff,
After seeing the data you provided im assuming the data set it something like
y4=[5 5 4 4 3 3 2 2 2];
t4=[1 2 3 4 5 6 7 8 9]; % this is not the actual data but only for better...
% understanding of how I see your problem
And the solution you are searching for is like
y4= [ 5 4 3 2]
answer= [ 2 4 6 9] % the maximum value of each t4 for a unique y4 value
Then the below solution should help you
N = diff([0 find(diff(y4)) numel(y4)]) %NOTE y4 should be a horizontal vector
for i=1:numel(N)

  0 件のコメント

サインイン to comment.

Translated by