Path: news.mathworks.com!not-for-mail
From: "helper " <spamless@nospam.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: newbie seeks vectorization help
Date: Sat, 10 May 2008 22:49:04 +0000 (UTC)
Organization: Timothy S. Farajian, Inc.
Lines: 100
Message-ID: <g058p0$bq3$1@fred.mathworks.com>
References: <g000e3$eoi$1@fred.mathworks.com> <g053lr$rss$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 1210459744 12099 172.30.248.35 (10 May 2008 22:49:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 10 May 2008 22:49:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1272923
Xref: news.mathworks.com comp.soft-sys.matlab:467741


"Roger Stafford" 
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in 
message <g053lr$rss$1@fred.mathworks.com>...
> "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
> 


I must admit, I didn't quite follow her paragraph 
explanation.  So I chose to ignore the paragraph and just  
vectorize the code.

Now that I reread, and see that S and I are logicals and 
not matrices of indices (as I gave in my example) I 
recognize the same issues as Roger.

His method is better, and there will be no need to convert 
to double since the multiplication between logical and 
double matrices will automatically convert to double.