Question

I have two vectors of the same size. The first one can have any different numbers with any order, the second one is decreasing (but can have the same elements) and consists of only positive integers. For example:

a = [7 8 13 6];
b = [5 2 2 1];

I would like to plot them in the following way: on the x axis I have points from a vector and on the y axis I have the sum of elements from vector b before this points divided by the sum(b). Therefore I will have points:

(7; 0.5) - 0.5 = 5/(5+2+2+1)
(8; 0.7) - 0.7 = (5+2)/(5+2+2+1)
(13; 0.9) ...
(6; 1) ...

I assume that this explanation might not help, so I included the imageenter image description here

Because this looks to me as a cumulative distribution function, I tried to find luck with cdfplot but with no success.

I have another option is to draw the image by plotting each line segment separately, but I hope that there is a better way of doing this.

Was it helpful?

Solution

I find the values on the x axis a little confusing. Leaving that aside for the moment, I think this does what you want:

b = [5 2 2 1];
stairs(cumsum(b)/sum(b));
set(gca,'Ylim',[0 1])

And if you really need those values on the x axis, simply rename the ticks of that axis:

a = [7 8 13 6];
set(gca,'xtick',1:length(b),'xticklabel',a)

Also grid on will add grid to the plot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top