How to produce a matrix that averages the top 5 values, for a corresponding year?

1 回表示 (過去 30 日間)
Naveed Hossain
Naveed Hossain 2018 年 5 月 10 日
コメント済み: KSSV 2018 年 5 月 10 日
I would like to produce an average of the top 5 values (on the 5th column, blue) for the corresponding year. I have attached my initial data as an attachment. My difficulty is that the matrix has multiple columns, so how do I choose that column (circled in blue), to calculate averages?

採用された回答

KSSV
KSSV 2018 年 5 月 10 日
Let A be your data of dimensions n*4. A has n number of rows and four columns. YOu can extract fourth column from A using.
flows = A(:,4) ; % This gives you fourth column
years = A(:,1) ; % This extracts the years from the data
% GEt mean of top five elements
iwant = mean(flows(1:5)) ;
If you have multiple years in the data, then you need to extract the respective year flows and get mean of top five values.
flows_1938 = flows(years==1938) ; % This gives flows of the year 1938
iwant_1938 = mean(flows(1:5)) ;
  2 件のコメント
Naveed Hossain
Naveed Hossain 2018 年 5 月 10 日
Would you happen to know how to produce a matrix, where it would look like the following:
Year Flows
1938 (Top 5 Average for 1938)
1939 (Top 5 average for 1939)
1940 etc
end
KSSV
KSSV 2018 年 5 月 10 日
flows = A(:,4) ; % This gives you fourth column
years = A(:,1) ; % This extracts the years from the data
% GEt years alone
[Y,ia,ib] = unique(years) ;
NY = length(Y) ; % number of years
iwant = zeros(N,2) ;
for i =1:n
F = flows(years==Y(i)) ;
if length(F)>5
iwant(i,:) = [Y(i) mean(F(1:5))] ;
else
iwant(i,:) = [Y(i) mean(F)] ;
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by