Select certain percentage of a plot
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Toan Vu
am 11 Feb. 2019
Bearbeitet: Kevin Phung
am 12 Feb. 2019
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
0 Kommentare
Akzeptierte Antwort
Kevin Phung
am 11 Feb. 2019
Bearbeitet: Kevin Phung
am 12 Feb. 2019
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 Kommentare
Kevin Phung
am 12 Feb. 2019
Bearbeitet: Kevin Phung
am 12 Feb. 2019
oops, i forgot to add a 'hold on' into the loop, I've edited my ans
Weitere Antworten (1)
aara
am 11 Feb. 2019
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!