Percentage of data in given range
14 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
dpb
2020-8-25
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!