フィルターのクリア

finding the mean and max

1 回表示 (過去 30 日間)
johnson saldanha
johnson saldanha 2018 年 11 月 21 日
編集済み: Andrei Bobrov 2018 年 11 月 21 日
suppose i have a matrix where the first column is x(:,1)=[ 1 1 1 2 2 1 1 3 1 ] and second column is x(:,2)=[ 2 3 2 4 6 9 7 8 9];
i want the output as y(:,1)=[ 1 2 3 ], y(:,2)=[ 9 6 8] and y(:,3)=[5.33 5 8];
the first column should be the values from first column of x reduced to a single value and in ascending order, the second column should be the highest number from the second column of x for a particular value in first input column. for ex. for all the 1's in first column of x should return one max value from the second column of x. the third column should contain the means of the second column of x for a particular value in first column of x. same example as given.

採用された回答

Rik
Rik 2018 年 11 月 21 日
編集済み: Rik 2018 年 11 月 21 日
The first column you can achieve with unique, the second and third can be done with accumarray.
x=[ 1 1 1 2 2 1 1 3 1 ; 2 3 2 4 6 9 7 8 9]';
[col1,~,ind]=unique(x(:,1));
col2=accumarray(ind,x(:,2),[],@max);
col3=accumarray(ind,x(:,2),[],@mean);
y=[col1 col2 col3]
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 21 日
the third output of unique gives you information about the original order .
Rik
Rik 2018 年 11 月 21 日
This code produces the output like you describe. As Walter mentions (and as you can read in the doc of unique) the third output of the unique function returns the order so isequal(col1(ind),x(:,1)) returns true (barring any float rounding errors).
This means that this input will also work as intended:
x=[5 1 1 1 2 2 1 1 3 1 5;9 2 3 2 4 6 9 7 8 9 1]';

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2018 年 11 月 21 日
編集済み: Andrei Bobrov 2018 年 11 月 21 日
EDITED
>> XXX =[ 1 1 1 2 2 1 1 3 1 ; 2 3 2 4 6 9 7 8 9]'
XXX =
1 2
1 3
1 2
2 4
2 6
1 9
1 7
3 8
1 9
>> T = varfun(@(x)[max(x),mean(x)],array2table(XXX),'G',1);
out = T{:,[1,3]}
out =
1 9 5.3333
2 6 5
3 8 8
>>
  2 件のコメント
johnson saldanha
johnson saldanha 2018 年 11 月 21 日
Error using getVarIndices (line 27)
Unrecognized variable name 'x1'.
Error in table/varfun (line 104)
groupVars = getVarIndices(a,groupVars);
Error in rider_pattern_mod_n (line 241)
T = varfun(@(xc)[max(xc),mean(xc)],array2table(xc),'G','x1');
got this error
Andrei Bobrov
Andrei Bobrov 2018 年 11 月 21 日
編集済み: Andrei Bobrov 2018 年 11 月 21 日
I edited my answer, and you read the help about the MATLAB functions.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by