Trying to sort a table by entry in specific column

1 view (last 30 days)
I am working on a problem were I need to take the mean of two variables from several trials in a table. I was trying to set up a for loop based on the entry in the first column having constant values (example, all the 1's) to take the mean of column 2 and column 3 for each constant value (all the 1's in the first column). I ran into issues making getting to loop to work, and was wondering if anyone had any suggestions for it.

Answers (2)

Peter Perkins
Peter Perkins on 23 Jul 2015
If this is a table in the sense of the table data type, it's even easier:
>> A=array2table([1 2 3;0 4 5;1 8 7;1 14 10;0 1 2;4 1 2;1 2 33])
A =
Var1 Var2 Var3
____ ____ ____
1 2 3
0 4 5
1 8 7
1 14 10
0 1 2
4 1 2
1 2 33
>> varfun(@mean,A,'GroupingVariable','Var1')
ans =
Var1 GroupCount mean_Var2 mean_Var3
____ __________ _________ _________
0 0 2 2.5 3.5
1 1 4 6.5 13.25
4 4 1 1 2

Azzi Abdelmalek
Azzi Abdelmalek on 21 Jul 2015
A=[1 2 3;0 4 5;1 8 7;1 14 10;0 1 2;4 1 2;1 2 33]
idx=A(:,1)==1
out=mean(A(idx,[2 3]),2)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!