calculate sum and average of rows that share a common identifier
1 view (last 30 days)
Show older comments
I have an m by n matrix S and an m x 1 vector N with identifiers, one number (integer) in N for each row in S.
I want to calculate the average for those rows in S that have the same identifier in N.
Example: S=[1 2; 3 4; 5 6; 7 8; 9 10];
N=[1 6 6 53 53]';
Desired result: A=[1 2 ; 4 5 ; 8 9]
A(1,:)=average of row 1 in S
A(2,:)=average of rows 2 and 3 in S (they have a common identifier "6")
A(3,:)=average of rows 4 and 5 in S (they have a common identifier "53")
I can of course run a for loop, but it is quite a bit of data, so I thought it might be possible to use matlab's clever matrix notation.
Any suggestions?
0 Comments
Answers (0)
See Also
Categories
Find more on Logical 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!