Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: newbie seeks vectorization help
Date: Sat, 10 May 2008 21:22:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 63
Message-ID: <g053lr$rss$1@fred.mathworks.com>
References: <g000e3$eoi$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210454523 28572 172.30.248.38 (10 May 2008 21:22:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 10 May 2008 21:22:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:467730


"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!
-----------
  In my version of matlab I couldn't get your for-loop to work properly.  I had 
to write it as:

 T = zeros(N,M); % (totalfactor)
 for m = 1:
  T(S(:,m),m) = sum(K(I(:,m),S(:,m)),1)';
 end

and even then there were cases that still gave it trouble.  The problem is that 
the right side vector in general has fewer elements than the left side in the 
form you defined, and matlab, at least mine, doesn't know which elements go 
where.

  In any event, when you say, "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", I assume that you meant if S(n,m) is false (zero,) 
then zero goes into the matrix at (n,m), regardless of what sum from K is 
obtained.  If so, the following is a possible vectorization of this, though I am 
not sure it is any faster than your for-loop method.

 totalfactor = S.*(K'*I);

The summation is done by the matrix product.  This uses your logical arrays 
as numerical arrays with 1's and 0's, so it is possible you would need to 
convert them explicitly to numerical type to work properly, say with '+'.  I 
don't know.  My own system doesn't know about logical types and so has no 
trouble with that.

Roger Stafford