Percentage of data in given range
조회 수: 18 (최근 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!