How to vectorize the sum of point products between a normal and a set of points

I need to evaluate the following expression (in pseudo-mathematical notation):

โˆ‘ i p i โ‹… n

where p is the matrix of three-element vectors, and n is a three-element vector. I can do this using loops as follows, but I cannot figure out how to vectorize this:

 p = [1 1 1; 2 2 2]; n = [3 3 3]; s = 0; for i = 1:size(p, 1) s = s + dot(p(i, :), n) end 
+4
source share
3 answers

Why complicate things? How about a simple matrix multiplication:

 s = sum(p * n(:)) 

where p is considered an M-3 matrix.

+5
source

I think you can do it with bsxfun :

 sum(sum(bsxfun(@times,p,n))) 
0
source
 ---------- % Is it the same for this case? ---------- n = 200; % depending on the computer it might be m = 1000*n; % that n needs to be chosen differently A = randn(n,m); x = randn(n,1); p = zeros(m,1); q = zeros(1,m); tic; for i = 1:m p(i) = sum(x.*A(:,i)); q(i) = sum(x.*A(:,i)); end time = toc; disp(['time = ',num2str(time)]); 
0
source

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


All Articles