Retrieving minimum values for parts of a matrix

1 回表示 (過去 30 日間)
Isma_gp
Isma_gp 2016 年 5 月 17 日
編集済み: Matt J 2016 年 5 月 17 日
Hi, I have a matrix containing different rows with values for several years. I need to retrieve the minimum value corresponding to each year. For example: A=[ 1958 2; 1958 3; 1959 4; 1959 5; 1959 6 ] The result matrix that I need would be: B=[ 1958 2; 1959 4 ]
Could you please help with this? Thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 17 日
編集済み: Azzi Abdelmalek 2016 年 5 月 17 日
A=[ 1958 2; 1958 3; 1959 4; 1959 5; 1959 6 ]
[ii,jj,kk]=unique(A(:,1))
out=[ii accumarray(kk,(1:numel(kk))',[],@(x) min(A(x,2)))]

その他の回答 (1 件)

Matt J
Matt J 2016 年 5 月 17 日
編集済み: Matt J 2016 年 5 月 17 日
[i,~,s]=find( accumarray(A(:,1), A(:,2),[],@min ) );
B=[i,s]
  1 件のコメント
Matt J
Matt J 2016 年 5 月 17 日
編集済み: Matt J 2016 年 5 月 17 日
You can be a bit more memory-conservative by making modifications like this,
[i,j,s]=find( accumarray(A(:,1)-1949, A(:,2),[],@min ) );
B=[i+1949,s]

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

カテゴリ

Help Center および File ExchangeLinear Programming and Mixed-Integer Linear Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by