subtracting matrices with different dimensions
Mostrar comentarios más antiguos
Hello im just practicing some stuff on Matlab and I have a stupid question.
So i have two matrices Xtest and Xmean.
Xtest is 45 x 4
Xmean is 1x4 which is lets say [1,2,3,4]
I want to subtract 1 from each row of column 1 of Xtest and 2 from each row of column 2 and so on.
What would be the simplest way to go about this. i cant just do Xtest - Xmean because the dimensions are wrong.
Respuestas (1)
Star Strider
el 26 de Abr. de 2016
Use the bsxfun function. It does exactly what you want:
Xtest = randi(9, 45, 4);
Xmean = [1,2,3,4];
Result = bsxfun(@minus, Xtest, Xmean);
The bsxfun function expands the singleton dimension of ‘Xmean’ to the dimensions of ‘Xtest’ and then does the subtraction.
You could do the same thing by using repmat on ‘Xmean’ and then doing the subtraction, but bsxfun is much more efficient and much faster.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!