How to take an average from range of values?

9 ビュー (過去 30 日間)
Haya Ali
Haya Ali 2023 年 3 月 17 日
コメント済み: Dyuman Joshi 2023 年 3 月 17 日
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]

採用された回答

Dyuman Joshi
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))
B = 1×16
-0.2463 -0.0352 0 0 0 0 0 0 0 0 0 0 0 0 0.3716 0.4341
avg = mean(B)
avg = 0.0328
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 件のコメント
Haya Ali
Haya Ali 2023 年 3 月 17 日
Is this a way to find a range also in which most values lies?
Dyuman Joshi
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)
idx = 7
%Range with maximum number of elements
bin(idx:idx+1)
ans = 1×2
0 0.5000
The range can be verified from the histrogram as well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by