How to take an average from range of values?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have this array and according to excel histogram plot the mostly values are located in the range [-0.5,0.5]. I want to sort all values from the array A that lies in this range and take there average. How can I do this?
A= [0.0000 0.4341 -0.0000 -0.5910 -0.0352 2.0350 -0.0000 -0.9597 0.0000 -1.2164 -2.7826 -0.0000 0.3716 -0.0000 -0.0000 -0.0000 1.4557 0.0000 -0.0000 0.5599 -0.0000 -0.2463 -0.7001 0.0000]
댓글 수: 0
채택된 답변
Dyuman Joshi
2023년 3월 17일
It seems you have copied the data from Excel and there is a loss of data in doing that.
A= [0.0000 0.4341 -0.0000 -0.5910 -0.0352 2.0350 -0.0000 -0.9597 0.0000 -1.2164 -2.7826 -0.0000 0.3716 -0.0000 -0.0000 -0.0000 1.4557 0.0000 -0.0000 0.5599 -0.0000 -0.2463 -0.7001 0.0000];
%range
idx = A>=-0.5 & A<=0.5;
%sorted A values in the range
B = sort(A(idx))
avg = mean(B)
Note that the range containing maximum number of elements can vary according to the bin size and the start point.
For example -
%bin size 0.5, start point -3.5, the range [0 0.5] has the max no. of elements
bin=-3.5:0.5:3.5;
histogram(A,bin)
xticks(bin)
%bin size - 0.3, start point -3.2, range with max elements is [-0.2 0.1]
bin=-3.2:0.3:3;
histogram(A,bin)
xticks(bin)
댓글 수: 2
Dyuman Joshi
2023년 3월 17일
It will depend upon the size and the starting point of the range, as I said above.
Let's say the bin size is 0.5, and the starting and end points are -3 and 3 respectively
A = [0.0000 0.4341 -0.0000 -0.5910 -0.0352 2.0350 -0.0000 -0.9597 0.0000 -1.2164 -2.7826 -0.0000 0.3716 -0.0000 -0.0000 -0.0000 1.4557 0.0000 -0.0000 0.5599 -0.0000 -0.2463 -0.7001 0.0000];
bin = -3:0.5:3;
h = histogram(A, bin);
xticks(bin)
%index of the maximum value
[~,idx] = max(h.Values)
%Range with maximum number of elements
bin(idx:idx+1)
The range can be verified from the histrogram as well.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!