Percentage of data in given range

18 views (last 30 days)
F Sp
F Sp on 25 Aug 2020
Commented: F Sp on 25 Aug 2020
Hello, I'm very sorry to ask since it seems very basic, but I didn't find any useful information on it.
So I have a 1D array. What I want is to give an interval [mean-x,mean+x] and calculate the percentage how many values of all are in this regime. I can easily code this in a few lines, but I was wondering if there is a shorter, more elegant way of calculating it. I checked pdf, paramci (kinda the opposite of what I wanna do) and so on.

Accepted Answer

dpb
dpb on 25 Aug 2020
Not a defined function, no. There are a couple of related in the Statistics Toolbox for percentiles, etc., but not what you're looking for, precisely.
But, it's simple enough with logical addressing
pct=sum(iswithin(x-mean(x)),-lim,lim)/numel(x)*100;
where iswithin is my utility function
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
You don't really need the function, of course, it's just "syntactic sugar" to move the compound expression out of the main code...which can come in really handy for more complex cases.
Above does assume a vector in the usage; only a little extra can extend it for arrays.
  1 Comment
F Sp
F Sp on 25 Aug 2020
Hey thanks, that's good to know. I thought I was overseeing summit

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!