MATLAB: Extract values within interquartile range in array

arraystatistics

Hi,
I want to extract values within an interquartile range in an array. Let say that the array looks as follows: v= [1 2 3 4 5 6 ……95 96 97 98 99]. The interquartile range is 25 and 75. How can I extract the array values from 25 to 75 to a new array? If possible, can I select other ranges than bottom and top 25%?
Thanks!

Best Answer

Use the quantile command to get whichever quantiles you want.
Then extract using logical indexing (as described on this documentation page).
v = 1:99;
q = quantile(v,[0.25 0.75])
v2 = v(v>q(1) & v<q(2))