calculate sum and average of rows that share a common identifier
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!