Row normalization row

Is there an efficient (calculation speed + number of keystrokes) way to perform normalization of strings in MATLAB using the built-in functions?

Here is what I have come up with so far

A = rand(m, n); % m rows, n cols v = pdist2(zeros(1, size(A, 2)), A); normA = diag(1./v) * A; 
+4
source share
1 answer

Assuming the row sums should be 1:

 bsxfun(@times, A, 1./(sum(A, 2))) 

Edit

If you are looking for the norm l2, as @Oli suggests, then

 bsxfun(@times, A, 1./sqrt(sum(A.^2, 2))) 

In this case, you can semi-gracefully process the sums of zero rows by doing

 bsxfun(@times, A, 1./(max(sum(A, 2), eps))) 
+7
source

Source: https://habr.com/ru/post/1385814/


All Articles