Reputation: 407
It is a column normalizaton. For example,
X= 1 -1 -2
1 4 -8
bsxfun(@rdivide,abs(X),sum(abs(X)))
will get
ans = 0.5 0.2 0.2
0.5 0.8 0.8
Upvotes: 1
Views: 119
Reputation: 4563
There is no built-in function to shorten your code, like Suever commented already. However, since MATLAB R2016b there is implicit expansion, also called broadcasting in other numerical languages/packages. This means your code can be shortened to abs(X)./sum(abs(X))
.
For the sake of explicitness the use of bsxfun
is better, although it can be more difficult to understand for those who don't know MATLAB. Implicit expansion is...implicit, though, which may lead to incorrect behaviour where you (or a reader of your code) assumes a different result or an error.
Upvotes: 3