subtracting matrices with different dimensions
이전 댓글 표시
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.
답변 (1개)
Star Strider
2016년 4월 26일
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.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!