Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: vectorization question Date: Mon, 13 Jun 2011 16:18:02 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 16 Message-ID: <it5d7q$1av$1@newscl01ah.mathworks.com> References: <it4ltl$n6p$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-06-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1307981882 1375 172.30.248.38 (13 Jun 2011 16:18:02 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 13 Jun 2011 16:18:02 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:731707 "Peter Schreiber" <schreiber.peter15@gmail.com> wrote in message <it4ltl$n6p$1@newscl01ah.mathworks.com>... > for counter = 1:max_count > A=(P5(:,counter)-P4(:,counter))/norm(P5(:,counter)-P4(:,counter)); > As=repmat(A',n_size_of ,1) - repmat(2*sum(N.*(repmat(A',n_size_of ,1)),2),1,3).*N; > end - - - - - - - - My guess is you are doing ray tracing off reflective surfaces and N is a set of 5000 unit normals to surface points. I guess further that you would actually like to have a max_count of 1000 instead of 10 since that is the size of P4 and P5 rows. Your computation can be vectorized to produce a 3 x 5000 x 1000 3D array of (ray?) vectors using 'bsxfun' (similar to Matt's code) provided you have enough memory to store it. Otherwise you would have to cut down the 1000 to lower values, as you have done. Also I am not sure this would be any faster than something involving for-loops, but you can test that yourself. A = P5-P4; % 3 x 1000 A = bsxfun(@rdivide,A,sqrt(sum(A.^2,1))); % 3 x 1000) As = bsxfun(@times,reshape(N*(2*A),1,5000,1000),N.'); % 3 x 5000 x 1000 As = bsxfun(@minus,reshape(A,3,1,1000),As) % 3 x 5000 x 1000 Roger Stafford