How to subtract mean along rows?
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (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 Kommentar
JFz
am 8 Jan. 2018
Kategorien
Mehr zu Mathematics and Optimization finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!