Select certain percentage of a plot

조회 수: 9 (최근 30일)
Toan Vu
Toan Vu 2019년 2월 11일
편집: Kevin Phung 2019년 2월 12일
Hi all,
I was wondering, how can I select certain points which contains a % desired amount.
To make it a bit clearer, imagine a x,y plot where
x = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
y = [0 1 3 1 2 0 0 0 2 1 2 2 4 5 6 7 8 9 11 20]
I want to find that point where y reaches for example 10% of the sum of y (which should be about 8.4 in this case). This should be between x = 8 and x = 9.
How can I give Matlab a command to find it for a certain percentage and the value of the x-axis? And is it also possible to indicate the value in the plot itself?
Thanks in forward

채택된 답변

Kevin Phung
Kevin Phung 2019년 2월 11일
편집: Kevin Phung 2019년 2월 12일
x = [ 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 20];
y = [0 1 3 1 2 0 0 0 2 1 2 2 4 5 6 7 8 9 11 20];
percent = .1;
thresh = percent*sum(y);
[closest_dist, idx] = min(abs(x-thresh));
x_vals = x(ismember(x,x(idx)));
y_vals = y(ismember(x,x(idx)));
will give you your x and y values. This code takes into account duplicates.
as for indicating it in the plot, you can plot each xy pair as its own line object in a for loop
line_objs= gobjects(1,numel(x));
for i = 1:numel(x)
p = plot(x(i),y(i),'*b')
line_objs(i) = p % store in graphics array
hold on;
end
%change color of the specific point(s):
line_objs(idx).Color = [1 0 0] % to red
*note: your x and y are different lengths. you should double check it and make sure they match
  댓글 수: 2
Toan Vu
Toan Vu 2019년 2월 12일
Yes I got it, thanks! However, for the second part, if I run that, then it will only give a single point in the plot, without the actual plot and that point is not at the right spot, how can I fix that?
Kevin Phung
Kevin Phung 2019년 2월 12일
편집: Kevin Phung 2019년 2월 12일
oops, i forgot to add a 'hold on' into the loop, I've edited my ans

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

aara
aara 2019년 2월 11일
You can use a for loop add all the totals together incrementally and check when its above your desired value:
x = 1:20;
y = [0 1 3 1 2 0 0 0 2 1 2 2 4 5 6 7 8 9 11 20];
total = 0;
percent = 10; %set percentage.
for i=1:length(y)
total = sum(y(1:i)); %this adds more y values together with each iteration
if total > sum(y)*percent/100
disp(x(i)) %x is only displayed when the sum values of y are bigger than percentage of y
break
end
end
Or if you need to integrate for area under the curve then:
You can use the trapz function in matlab (this gives different results) :
x = 1:20;
y = [0 1 3 1 2 0 0 0 2 1 2 2 4 5 6 7 8 9 11 20];
total = 0; %this is to add the total area under the curve
percent = 10;
for i=1:length(y)
total = total+trapz(y(1:i)); %with each iteration, the integration area increases
if total >= sum(y)*percent/100 %if the area under the curve passes your required percent then it would display corresponding x value and stop the loop.
disp("X-value: "+ x(i))
break
end
end

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by