Path: news.mathworks.com!not-for-mail
From: "helper " <spamless@nospam.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: newbie seeks vectorization help
Date: Fri, 9 May 2008 11:00:25 +0000 (UTC)
Organization: Timothy S. Farajian, Inc.
Lines: 41
Message-ID: <g01as9$pf6$1@fred.mathworks.com>
References: <g000e3$eoi$1@fred.mathworks.com>
Reply-To: "helper " <spamless@nospam.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210330825 26086 172.30.248.35 (9 May 2008 11:00:25 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 9 May 2008 11:00:25 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1272923
Xref: news.mathworks.com comp.soft-sys.matlab:467575


"Sky Pelletier" <skytoddk@remove14chars.vet.upenn.edu> 
wrote in message <g000e3$eoi$1@fred.mathworks.com>...
> I am brand-spanking new to vectorizing code.  Forgive my
> ignorance.
> 
> N indicates number of nodes.
> M indicates number of simulations.
> 
> K = an N x N matrix of pre-calculated between-node
> interaction values for this application. 
> 
> I have two N x M logical matrices, S and I, where a 1 in
> position (n,m) indicates that in simulation M, node n is
> active in set S or I as appropriate.  The goal is to
> generate an N x M matrix in which element (n,m) is the sum
> of effect of all active nodes in I on the n'th node in S,
> for simulation M.
> 
> my code is like:
> 
> totalfactor = zeros(N,M);
> 
> for simnumber = 1:M
>   totalfactor(:,simnumber) = sum( K( I(:, simnumber), S(:,
> simnumber), 1);
> end
> 
> I KNOW there is a more elegant (and faster!) way to do 
this,
> but I am just learning this vectorization and my brain is
> having a hard time switching from the looping I'm used to.
> 
> Thank you for your help!  I'm not used to feeling so 
ignorant!


I suspect that using your FOR-loop may in fact be the 
fastest and most elegant (and most memory efficient) way of 
doing this.

This can often be the case with MATLAB.