Trying to sort a table by entry in specific column
이전 댓글 표시
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.
답변 (2개)
Peter Perkins
2015년 7월 23일
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
2015년 7월 21일
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)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!