How to subtract mean along rows?
Mostrar comentarios más antiguos
Hi,
I have a matrix 100x120 and I can group along the rows by findgroups. Then I use mean_mat = splitapply(@mean, mat, G) to get the means along rows. How to subtract the mean from the original matrix from each group? That is: for each group I, get mat_i - mean_mat_i? Thanks.
Respuesta aceptada
Más respuestas (2)
A simpler way, which does not involve loops at all, just simple indexing:
mean_mat = splitapply(@mean, mat, G);
mat_minus_mean = mat - mean_mat(sub2ind(size(mean_mat), repmat(G, 1, size(meat, 2)), repmat(1:size(mat, 2), numel(G), 1)));
The mean_mat(sub2ind(...)) simply creates a matrix the same size as mat whose row r is mean_mat(G(r), :). This is then simply subtracted from mat.
1 comentario
JFz
el 8 de En. de 2018
Categorías
Más información sobre Mathematics and Optimization en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!