<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970</link>
    <title>MATLAB Central Newsreader - newbie seeks vectorization help</title>
    <description>Feed for thread: newbie seeks vectorization help</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 11 May 2008 06:39:05 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431425</link>
      <author>Roger Stafford</author>
      <description>"Sky Pelletier" &amp;lt;skytoddk@remove14chars.vet.upenn.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g0628u$qdp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Roger,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; totalfactor = S.*(K'*I);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; is exactly what I was looking for!  Thank you!  I was&lt;br&gt;
&amp;gt; suffering from tunnel vision; having indexed into the matrix&lt;br&gt;
&amp;gt; K in an earlier version, I was focusing on how to use the&lt;br&gt;
&amp;gt; logical matrices S and I to index K, when in fact my&lt;br&gt;
&amp;gt; original idea was to use them as multiplicative masks--I&lt;br&gt;
&amp;gt; even call them S_mask and I_mask in my code!  An&lt;br&gt;
&amp;gt; intermediate  version had S and I as index vectors, and I&lt;br&gt;
&amp;gt; was trying so hard to figure out how to adjust that version&lt;br&gt;
&amp;gt; while still using them as indices that I missed the obvious.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and by the way, I think my loop actually doesn't work&lt;br&gt;
&amp;gt; because of how I misunderstood the matrix versus linear&lt;br&gt;
&amp;gt; indexing issue.  In any event, it wasn't giving the correct&lt;br&gt;
&amp;gt; answers when I left work Friday :)  I believe with a few&lt;br&gt;
&amp;gt; appropriate tweaks I should be able to get it working so I&lt;br&gt;
&amp;gt; can test which is actually faster, though I strongly suspect&lt;br&gt;
&amp;gt; the method you proposed will prove to be the winner.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you so much.  I owe you a coke.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ..Sky&lt;br&gt;
------------&lt;br&gt;
Hello Sky Pelletier,&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;You are entirely welcome.  To tell the truth I didn't find that solution right &lt;br&gt;
away.  I was trying for a for-loop along the n=1:N direction, just in case your &lt;br&gt;
N was appreciably smaller than your M, but when I laid it out that way, the &lt;br&gt;
total vectorization method just fell into my lap, so to speak.  It was begging &lt;br&gt;
to be used, and I don't know why I hadn't seen it earlier.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sun, 11 May 2008 06:04:14 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431424</link>
      <author>Sky Pelletier</author>
      <description>Roger,&lt;br&gt;
&lt;br&gt;
totalfactor = S.*(K'*I);&lt;br&gt;
&lt;br&gt;
is exactly what I was looking for!  Thank you!  I was&lt;br&gt;
suffering from tunnel vision; having indexed into the matrix&lt;br&gt;
K in an earlier version, I was focusing on how to use the&lt;br&gt;
logical matrices S and I to index K, when in fact my&lt;br&gt;
original idea was to use them as multiplicative masks--I&lt;br&gt;
even call them S_mask and I_mask in my code!  An&lt;br&gt;
intermediate  version had S and I as index vectors, and I&lt;br&gt;
was trying so hard to figure out how to adjust that version&lt;br&gt;
while still using them as indices that I missed the obvious.&lt;br&gt;
&lt;br&gt;
and by the way, I think my loop actually doesn't work&lt;br&gt;
because of how I misunderstood the matrix versus linear&lt;br&gt;
indexing issue.  In any event, it wasn't giving the correct&lt;br&gt;
answers when I left work Friday :)  I believe with a few&lt;br&gt;
appropriate tweaks I should be able to get it working so I&lt;br&gt;
can test which is actually faster, though I strongly suspect&lt;br&gt;
the method you proposed will prove to be the winner.&lt;br&gt;
&lt;br&gt;
Thank you so much.  I owe you a coke.&lt;br&gt;
&lt;br&gt;
..Sky&lt;br&gt;
&lt;br&gt;
"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt;&lt;br&gt;
wrote in message &amp;lt;g053lr$rss$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt;   In any event, when you say, "The goal is to generate an&lt;br&gt;
N x M matrix in &lt;br&gt;
&amp;gt; which element (n,m) is the sum of effect of all active&lt;br&gt;
nodes in I on the n'th &lt;br&gt;
&amp;gt; node in S, for simulation m", I assume that you meant if&lt;br&gt;
S(n,m) is false (zero,) &lt;br&gt;
&amp;gt; then zero goes into the matrix at (n,m), regardless of&lt;br&gt;
what sum from K is &lt;br&gt;
&amp;gt; obtained.  If so, the following is a possible&lt;br&gt;
vectorization of this, though I am &lt;br&gt;
&amp;gt; not sure it is any faster than your for-loop method.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  totalfactor = S.*(K'*I);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The summation is done by the matrix product.  This uses&lt;br&gt;
your logical arrays &lt;br&gt;
&amp;gt; as numerical arrays with 1's and 0's, so it is possible&lt;br&gt;
you would need to &lt;br&gt;
&amp;gt; convert them explicitly to numerical type to work&lt;br&gt;
properly, say with '+'.  I &lt;br&gt;
&amp;gt; don't know.  My own system doesn't know about logical&lt;br&gt;
types and so has no &lt;br&gt;
&amp;gt; trouble with that.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 10 May 2008 22:49:04 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431395</link>
      <author>helper </author>
      <description>"Roger Stafford" &lt;br&gt;
&amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in &lt;br&gt;
message &amp;lt;g053lr$rss$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Sky Pelletier" &amp;lt;skytoddk@remove14chars.vet.upenn.edu&amp;gt; &lt;br&gt;
wrote in message &lt;br&gt;
&amp;gt; &amp;lt;g000e3$eoi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I am brand-spanking new to vectorizing code.  Forgive my&lt;br&gt;
&amp;gt; &amp;gt; ignorance.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; N indicates number of nodes.&lt;br&gt;
&amp;gt; &amp;gt; M indicates number of simulations.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; K = an N x N matrix of pre-calculated between-node&lt;br&gt;
&amp;gt; &amp;gt; interaction values for this application. &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have two N x M logical matrices, S and I, where a 1 in&lt;br&gt;
&amp;gt; &amp;gt; position (n,m) indicates that in simulation M, node n is&lt;br&gt;
&amp;gt; &amp;gt; active in set S or I as appropriate.  The goal is to&lt;br&gt;
&amp;gt; &amp;gt; generate an N x M matrix in which element (n,m) is the &lt;br&gt;
sum&lt;br&gt;
&amp;gt; &amp;gt; of effect of all active nodes in I on the n'th node in &lt;br&gt;
S,&lt;br&gt;
&amp;gt; &amp;gt; for simulation M.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; my code is like:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; totalfactor = zeros(N,M);&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for simnumber = 1:M&lt;br&gt;
&amp;gt; &amp;gt;   totalfactor(:,simnumber) = sum( K( I(:, simnumber), S&lt;br&gt;
(:,&lt;br&gt;
&amp;gt; &amp;gt; simnumber), 1);&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I KNOW there is a more elegant (and faster!) way to do &lt;br&gt;
this,&lt;br&gt;
&amp;gt; &amp;gt; but I am just learning this vectorization and my brain &lt;br&gt;
is&lt;br&gt;
&amp;gt; &amp;gt; having a hard time switching from the looping I'm used &lt;br&gt;
to.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thank you for your help!  I'm not used to feeling so &lt;br&gt;
ignorant!&lt;br&gt;
&amp;gt; -----------&lt;br&gt;
&amp;gt;   In my version of matlab I couldn't get your for-loop to &lt;br&gt;
work properly.  I had &lt;br&gt;
&amp;gt; to write it as:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  T = zeros(N,M); % (totalfactor)&lt;br&gt;
&amp;gt;  for m = 1:&lt;br&gt;
&amp;gt;   T(S(:,m),m) = sum(K(I(:,m),S(:,m)),1)';&lt;br&gt;
&amp;gt;  end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and even then there were cases that still gave it &lt;br&gt;
trouble.  The problem is that &lt;br&gt;
&amp;gt; the right side vector in general has fewer elements than &lt;br&gt;
the left side in the &lt;br&gt;
&amp;gt; form you defined, and matlab, at least mine, doesn't know &lt;br&gt;
which elements go &lt;br&gt;
&amp;gt; where.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   In any event, when you say, "The goal is to generate an &lt;br&gt;
N x M matrix in &lt;br&gt;
&amp;gt; which element (n,m) is the sum of effect of all active &lt;br&gt;
nodes in I on the n'th &lt;br&gt;
&amp;gt; node in S, for simulation m", I assume that you meant if S&lt;br&gt;
(n,m) is false (zero,) &lt;br&gt;
&amp;gt; then zero goes into the matrix at (n,m), regardless of &lt;br&gt;
what sum from K is &lt;br&gt;
&amp;gt; obtained.  If so, the following is a possible &lt;br&gt;
vectorization of this, though I am &lt;br&gt;
&amp;gt; not sure it is any faster than your for-loop method.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  totalfactor = S.*(K'*I);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The summation is done by the matrix product.  This uses &lt;br&gt;
your logical arrays &lt;br&gt;
&amp;gt; as numerical arrays with 1's and 0's, so it is possible &lt;br&gt;
you would need to &lt;br&gt;
&amp;gt; convert them explicitly to numerical type to work &lt;br&gt;
properly, say with '+'.  I &lt;br&gt;
&amp;gt; don't know.  My own system doesn't know about logical &lt;br&gt;
types and so has no &lt;br&gt;
&amp;gt; trouble with that.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I must admit, I didn't quite follow her paragraph &lt;br&gt;
explanation.  So I chose to ignore the paragraph and just  &lt;br&gt;
vectorize the code.&lt;br&gt;
&lt;br&gt;
Now that I reread, and see that S and I are logicals and &lt;br&gt;
not matrices of indices (as I gave in my example) I &lt;br&gt;
recognize the same issues as Roger.&lt;br&gt;
&lt;br&gt;
His method is better, and there will be no need to convert &lt;br&gt;
to double since the multiplication between logical and &lt;br&gt;
double matrices will automatically convert to double.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 10 May 2008 21:22:03 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431384</link>
      <author>Roger Stafford</author>
      <description>"Sky Pelletier" &amp;lt;skytoddk@remove14chars.vet.upenn.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g000e3$eoi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I am brand-spanking new to vectorizing code.  Forgive my&lt;br&gt;
&amp;gt; ignorance.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; N indicates number of nodes.&lt;br&gt;
&amp;gt; M indicates number of simulations.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K = an N x N matrix of pre-calculated between-node&lt;br&gt;
&amp;gt; interaction values for this application. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have two N x M logical matrices, S and I, where a 1 in&lt;br&gt;
&amp;gt; position (n,m) indicates that in simulation M, node n is&lt;br&gt;
&amp;gt; active in set S or I as appropriate.  The goal is to&lt;br&gt;
&amp;gt; generate an N x M matrix in which element (n,m) is the sum&lt;br&gt;
&amp;gt; of effect of all active nodes in I on the n'th node in S,&lt;br&gt;
&amp;gt; for simulation M.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; my code is like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; totalfactor = zeros(N,M);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for simnumber = 1:M&lt;br&gt;
&amp;gt;   totalfactor(:,simnumber) = sum( K( I(:, simnumber), S(:,&lt;br&gt;
&amp;gt; simnumber), 1);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I KNOW there is a more elegant (and faster!) way to do this,&lt;br&gt;
&amp;gt; but I am just learning this vectorization and my brain is&lt;br&gt;
&amp;gt; having a hard time switching from the looping I'm used to.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for your help!  I'm not used to feeling so ignorant!&lt;br&gt;
-----------&lt;br&gt;
&amp;nbsp;&amp;nbsp;In my version of matlab I couldn't get your for-loop to work properly.  I had &lt;br&gt;
to write it as:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;T = zeros(N,M); % (totalfactor)&lt;br&gt;
&amp;nbsp;for m = 1:&lt;br&gt;
&amp;nbsp;&amp;nbsp;T(S(:,m),m) = sum(K(I(:,m),S(:,m)),1)';&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
and even then there were cases that still gave it trouble.  The problem is that &lt;br&gt;
the right side vector in general has fewer elements than the left side in the &lt;br&gt;
form you defined, and matlab, at least mine, doesn't know which elements go &lt;br&gt;
where.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;In any event, when you say, "The goal is to generate an N x M matrix in &lt;br&gt;
which element (n,m) is the sum of effect of all active nodes in I on the n'th &lt;br&gt;
node in S, for simulation m", I assume that you meant if S(n,m) is false (zero,) &lt;br&gt;
then zero goes into the matrix at (n,m), regardless of what sum from K is &lt;br&gt;
obtained.  If so, the following is a possible vectorization of this, though I am &lt;br&gt;
not sure it is any faster than your for-loop method.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;totalfactor = S.*(K'*I);&lt;br&gt;
&lt;br&gt;
The summation is done by the matrix product.  This uses your logical arrays &lt;br&gt;
as numerical arrays with 1's and 0's, so it is possible you would need to &lt;br&gt;
convert them explicitly to numerical type to work properly, say with '+'.  I &lt;br&gt;
don't know.  My own system doesn't know about logical types and so has no &lt;br&gt;
trouble with that.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 09 May 2008 21:50:21 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431300</link>
      <author>Sky Pelletier</author>
      <description>That helps enormously!  Even though the vectorization may&lt;br&gt;
come out slower, I feel I have a much better grasp of what's&lt;br&gt;
going on..  Thank you!&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
"helper " &amp;lt;spamless@nospam.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;g02b0v$o9v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I hope this helps.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 09 May 2008 20:09:03 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431291</link>
      <author>helper </author>
      <description>"Sky Pelletier" &amp;lt;skytoddk@remove14chars.vet.upenn.edu&amp;gt; &lt;br&gt;
wrote in message &amp;lt;g01rcb$28h$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thank you for your quick feedback!  I greatly appreciate &lt;br&gt;
it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; It seems like I should be able to find a clever way to &lt;br&gt;
index&lt;br&gt;
&amp;gt; directly at the cost of using more memory, but I just &lt;br&gt;
can't&lt;br&gt;
&amp;gt; wrap my head around it.  I don't mind too much trading off&lt;br&gt;
&amp;gt; memory for speed; this loop occurs inside an application&lt;br&gt;
&amp;gt; that ultimately will be run several billion times using&lt;br&gt;
&amp;gt; different input parameters, so speed is a priority--but &lt;br&gt;
you&lt;br&gt;
&amp;gt; think this might actually be the fastest way?  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there any way of a priori identifying when &lt;br&gt;
vectorization&lt;br&gt;
&amp;gt; is NOT a good idea?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks again for your help...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ..Sky&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
There is no definite a priori method of identifying when &lt;br&gt;
vectorization will be better than FOR-loops.  However, &lt;br&gt;
because of the speed of modern day processors, memory &lt;br&gt;
allocation can often be the primary source of performance &lt;br&gt;
problems (not arithmetic operations).  Therefore, if you &lt;br&gt;
have to allocate a lot of new memory just to vectorize your &lt;br&gt;
code, then it is generally not worth it.&lt;br&gt;
&lt;br&gt;
Ok, you asked for it:&lt;br&gt;
&lt;br&gt;
totalFactor = reshape(sum(bsxfun(@(X,Y)K(X,Y),kron(I,ones&lt;br&gt;
(1,N)),S(:).'),1),N,M);&lt;br&gt;
&lt;br&gt;
Much slower and MUCH less elegant.&lt;br&gt;
&lt;br&gt;
To help you wrap your head around what you are doing and &lt;br&gt;
how I vectorized it:&lt;br&gt;
&lt;br&gt;
The portion of your code:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;K( I(:, simnumber), S(:,simnumber))&lt;br&gt;
&lt;br&gt;
which, if we define:&lt;br&gt;
&lt;br&gt;
I = [&lt;br&gt;
&amp;nbsp;2 5&lt;br&gt;
&amp;nbsp;4 6&lt;br&gt;
&amp;nbsp;6 7];&lt;br&gt;
&lt;br&gt;
S = [&lt;br&gt;
&amp;nbsp;1 7&lt;br&gt;
&amp;nbsp;3 8&lt;br&gt;
&amp;nbsp;5 9];&lt;br&gt;
&lt;br&gt;
For simnumber=1, &lt;br&gt;
&lt;br&gt;
&amp;nbsp;K([2;4;6], [1;3;5])&lt;br&gt;
&lt;br&gt;
returns a submatrix of the 2,4,6 rows and the 1,3,5 &lt;br&gt;
columns.  Then, the command:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;sum(K([2;4;6], [1;3;5]), 1) % simnumber=1&lt;br&gt;
&lt;br&gt;
gives you the sum of of the 2,4,6th elements in each of the &lt;br&gt;
1,3,5 columns.  This syntax works only because for each of &lt;br&gt;
the 1,3,5 columns, you want the same rows.&lt;br&gt;
&lt;br&gt;
However, if we consider another another submatrix of data:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;sum(K([7;8;9], [5;6;7]),1) % simnumber=2&lt;br&gt;
&lt;br&gt;
and we want to perform this operation for both simnumber=1 &lt;br&gt;
and sumnumber=2 with a single subscripting operation, we &lt;br&gt;
are out of luck.  Subscripting only returns submatrices of &lt;br&gt;
data.  The simnumber=1 command requests a submatrix, and &lt;br&gt;
the simnumber=2 command requests a submatrix, but the two &lt;br&gt;
combined are no longer a submatrix. &lt;br&gt;
&lt;br&gt;
So now what you want are the following sums:&lt;br&gt;
&lt;br&gt;
row(I2)  column(S2)  &lt;br&gt;
&amp;nbsp;2 4 6      1&lt;br&gt;
&amp;nbsp;2 4 6      3&lt;br&gt;
&amp;nbsp;2 4 6      5&lt;br&gt;
&amp;nbsp;7 8 9      5&lt;br&gt;
&amp;nbsp;7 8 9      6&lt;br&gt;
&amp;nbsp;7 8 9      7&lt;br&gt;
&lt;br&gt;
These matrices are created using:&lt;br&gt;
&lt;br&gt;
I2 = kron(I,ones(1,3)).'&lt;br&gt;
S2 = S(:)&lt;br&gt;
&lt;br&gt;
One method for obtaining this scattering (non submatrix) of &lt;br&gt;
data would be to use indexing.  First, repeat the set of &lt;br&gt;
column data:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;subscripts&lt;br&gt;
row(I2)    column(S2)&lt;br&gt;
&amp;nbsp;2 4 6      1 1 1&lt;br&gt;
&amp;nbsp;2 4 6      3 3 3&lt;br&gt;
&amp;nbsp;2 4 6      5 5 5&lt;br&gt;
&amp;nbsp;7 8 9      5 5 5&lt;br&gt;
&amp;nbsp;7 8 9      6 6 6&lt;br&gt;
&amp;nbsp;7 8 9      7 7 7&lt;br&gt;
&lt;br&gt;
S2 = repmat(S2,1,3)&lt;br&gt;
&lt;br&gt;
Then, convert each row,column pair to an index using &lt;br&gt;
SUB2IND:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;index = sub2ind(size(K), I2, S2);&lt;br&gt;
&lt;br&gt;
Assuming K is 10-by-10, we get&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;index&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     4     6&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22    24    26&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;42    44    46&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;65    66    67&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;75    76    77&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;85    86    87&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
we can now index into K using:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;vals = K(index)&lt;br&gt;
&lt;br&gt;
and the equivalent summing command would be&lt;br&gt;
&lt;br&gt;
&amp;nbsp;sums = sum(K(index), 2)&lt;br&gt;
&lt;br&gt;
To now RESHAPE these sums back to a matrix where they &lt;br&gt;
correspond to what your original command:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;totalfactor(:,simnumber) = sum( K( I(:, simnumber), S&lt;br&gt;
(:,simnumber)), 1);&lt;br&gt;
&lt;br&gt;
gave, we use:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;totalfactor = reshape(sums,3, 2);&lt;br&gt;
&lt;br&gt;
Summarizing the code and substituting N=3, M=2:&lt;br&gt;
&lt;br&gt;
I2 = kron(I,ones(1,N)).';&lt;br&gt;
S2 = S(:);&lt;br&gt;
S2 = repmat(S2,1,N);&lt;br&gt;
index = sub2ind(size(K), I2, S2);&lt;br&gt;
sums = sum(K(index), 2)&lt;br&gt;
totalfactor = reshape(sums,N,M);&lt;br&gt;
&lt;br&gt;
we get a equivalent vectorized operation.  We can even &lt;br&gt;
combine all of this into one behemoth of a line of code.  &lt;br&gt;
Note, however, that this is less elegant and if you test it &lt;br&gt;
with tic/toc, you will see it is MUCH slower than your FOR-&lt;br&gt;
loop.&lt;br&gt;
&lt;br&gt;
This is not the same method I gave at the start.  The first &lt;br&gt;
set of code I gave is an attempt to avoid having to using &lt;br&gt;
REPMAT, and instead using BSXFUN, however we still do not &lt;br&gt;
compare in performance to your FOR-loop.&lt;br&gt;
&lt;br&gt;
Something useful to keep in mind:&lt;br&gt;
&lt;br&gt;
Subscripting "K(row,col)" only returns submatrices of K.  &lt;br&gt;
If you want a nonsubmatrix (scattered elements), you must &lt;br&gt;
use indexing "K(index)".&lt;br&gt;
&lt;br&gt;
I hope this helps.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 09 May 2008 15:42:03 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431264</link>
      <author>Sky Pelletier</author>
      <description>Thank you for your quick feedback!  I greatly appreciate it.&lt;br&gt;
&lt;br&gt;
It seems like I should be able to find a clever way to index&lt;br&gt;
directly at the cost of using more memory, but I just can't&lt;br&gt;
wrap my head around it.  I don't mind too much trading off&lt;br&gt;
memory for speed; this loop occurs inside an application&lt;br&gt;
that ultimately will be run several billion times using&lt;br&gt;
different input parameters, so speed is a priority--but you&lt;br&gt;
think this might actually be the fastest way?  &lt;br&gt;
&lt;br&gt;
Is there any way of a priori identifying when vectorization&lt;br&gt;
is NOT a good idea?&lt;br&gt;
&lt;br&gt;
Thanks again for your help...&lt;br&gt;
&lt;br&gt;
..Sky&lt;br&gt;
&lt;br&gt;
"helper " &amp;lt;spamless@nospam.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;g01as9$pf6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I suspect that using your FOR-loop may in fact be the &lt;br&gt;
&amp;gt; fastest and most elegant (and most memory efficient) way of &lt;br&gt;
&amp;gt; doing this.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This can often be the case with MATLAB.&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 09 May 2008 11:00:25 -0400</pubDate>
      <title>Re: newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431229</link>
      <author>helper </author>
      <description>"Sky Pelletier" &amp;lt;skytoddk@remove14chars.vet.upenn.edu&amp;gt; &lt;br&gt;
wrote in message &amp;lt;g000e3$eoi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I am brand-spanking new to vectorizing code.  Forgive my&lt;br&gt;
&amp;gt; ignorance.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; N indicates number of nodes.&lt;br&gt;
&amp;gt; M indicates number of simulations.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K = an N x N matrix of pre-calculated between-node&lt;br&gt;
&amp;gt; interaction values for this application. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have two N x M logical matrices, S and I, where a 1 in&lt;br&gt;
&amp;gt; position (n,m) indicates that in simulation M, node n is&lt;br&gt;
&amp;gt; active in set S or I as appropriate.  The goal is to&lt;br&gt;
&amp;gt; generate an N x M matrix in which element (n,m) is the sum&lt;br&gt;
&amp;gt; of effect of all active nodes in I on the n'th node in S,&lt;br&gt;
&amp;gt; for simulation M.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; my code is like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; totalfactor = zeros(N,M);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for simnumber = 1:M&lt;br&gt;
&amp;gt;   totalfactor(:,simnumber) = sum( K( I(:, simnumber), S(:,&lt;br&gt;
&amp;gt; simnumber), 1);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I KNOW there is a more elegant (and faster!) way to do &lt;br&gt;
this,&lt;br&gt;
&amp;gt; but I am just learning this vectorization and my brain is&lt;br&gt;
&amp;gt; having a hard time switching from the looping I'm used to.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for your help!  I'm not used to feeling so &lt;br&gt;
ignorant!&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I suspect that using your FOR-loop may in fact be the &lt;br&gt;
fastest and most elegant (and most memory efficient) way of &lt;br&gt;
doing this.&lt;br&gt;
&lt;br&gt;
This can often be the case with MATLAB.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 22:56:03 -0400</pubDate>
      <title>newbie seeks vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168970#431163</link>
      <author>Sky Pelletier</author>
      <description>I am brand-spanking new to vectorizing code.  Forgive my&lt;br&gt;
ignorance.&lt;br&gt;
&lt;br&gt;
N indicates number of nodes.&lt;br&gt;
M indicates number of simulations.&lt;br&gt;
&lt;br&gt;
K = an N x N matrix of pre-calculated between-node&lt;br&gt;
interaction values for this application. &lt;br&gt;
&lt;br&gt;
I have two N x M logical matrices, S and I, where a 1 in&lt;br&gt;
position (n,m) indicates that in simulation M, node n is&lt;br&gt;
active in set S or I as appropriate.  The goal is to&lt;br&gt;
generate an N x M matrix in which element (n,m) is the sum&lt;br&gt;
of effect of all active nodes in I on the n'th node in S,&lt;br&gt;
for simulation M.&lt;br&gt;
&lt;br&gt;
my code is like:&lt;br&gt;
&lt;br&gt;
totalfactor = zeros(N,M);&lt;br&gt;
&lt;br&gt;
for simnumber = 1:M&lt;br&gt;
&amp;nbsp;&amp;nbsp;totalfactor(:,simnumber) = sum( K( I(:, simnumber), S(:,&lt;br&gt;
simnumber), 1);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
I KNOW there is a more elegant (and faster!) way to do this,&lt;br&gt;
but I am just learning this vectorization and my brain is&lt;br&gt;
having a hard time switching from the looping I'm used to.&lt;br&gt;
&lt;br&gt;
Thank you for your help!  I'm not used to feeling so ignorant!&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
