finding the mean and max
Show older comments
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.
Accepted Answer
More Answers (1)
Andrei Bobrov
on 21 Nov 2018
Edited: Andrei Bobrov
on 21 Nov 2018
1 vote
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 Comments
johnson saldanha
on 21 Nov 2018
Andrei Bobrov
on 21 Nov 2018
Edited: Andrei Bobrov
on 21 Nov 2018
I edited my answer, and you read the help about the MATLAB functions.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!