http://www.mathworks.com/matlabcentral/newsreader/view_thread/309874
MATLAB Central Newsreader  Mixing and matching variables, speed up
Feed for thread: Mixing and matching variables, speed up
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 29 Jun 2011 09:09:09 +0000
Mixing and matching variables, speed up
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309874#843346
Floris Zoutman
I am trying to speed up part of my code but I cannot find a way to do it faster. This is the problem I am working on. I have 4 arrays: number and LKN_EN are each 67000*1. Hcat and LKN_ENcat are each 175000*11. I tried to combine them in the following way: <br>
My first try:<br>
tmp2=zeros(size(number,1),size(Hcat,2));<br>
for i=1:length(number)<br>
for j=1:size(Hcat,2) <br>
tmp(2i,j)=any(Hcat(:,j)==number(i)) && any(LKN_ENcat(:,j)==(LKN_EN(i)));<br>
end<br>
end<br>
This is obviously extremely slow due to the double loop and probably also due to the use of any, but as it turns out, my second try with a single loop is even slower for reasons that are entirely unclear to me because it seems pretty efficient!<br>
for i=1:length(number)<br>
tmp=Hcat==number(i) & LKN_ENcat==LKN_EN(i);<br>
tmp2(i,:)=sum(tmp);<br>
end<br>
Does anybody have a good suggestion to improve the code? Many thanks in advance

Wed, 29 Jun 2011 14:52:54 +0000
Re: Mixing and matching variables, speed up
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309874#843389
Steven_Lord
<br>
<br>
"Floris Zoutman" <fzoutman@hotmail.com> wrote in message <br>
news:iueq3l$hr2$1@newscl01ah.mathworks.com...<br>
> I am trying to speed up part of my code but I cannot find a way to do it <br>
> faster. This is the problem I am working on. I have 4 arrays: number and <br>
> LKN_EN are each 67000*1. Hcat and LKN_ENcat are each 175000*11. I tried to <br>
> combine them in the following way: My first try:<br>
> tmp2=zeros(size(number,1),size(Hcat,2));<br>
> for i=1:length(number)<br>
> for j=1:size(Hcat,2) tmp(2i,j)=any(Hcat(:,j)==number(i)) && <br>
> any(LKN_ENcat(:,j)==(LKN_EN(i)));<br>
> end<br>
> end<br>
> This is obviously extremely slow due to the double loop and probably also <br>
> due to the use of any, but as it turns out, my second try with a single <br>
> loop is even slower for reasons that are entirely unclear to me because it <br>
> seems pretty efficient!<br>
> for i=1:length(number)<br>
> tmp=Hcat==number(i) & LKN_ENcat==LKN_EN(i);<br>
> tmp2(i,:)=sum(tmp);<br>
> end<br>
> Does anybody have a good suggestion to improve the code? Many thanks in <br>
> advance<br>
<br>
Let's take a step back. Please explain, in words rather than code or <br>
equations, what you intend this code to do. I _think_ if I understand your <br>
code correctly ISMEMBER with the 'rows' flag will help you, but I'm not <br>
certain.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Thu, 30 Jun 2011 00:29:09 +0000
Re: Mixing and matching variables, speed up
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309874#843456
Roger Stafford
"Steven_Lord" <slord@mathworks.com> wrote in message <iufe85$fkg$1@newscl01ah.mathworks.com>...<br>
> "Floris Zoutman" <fzoutman@hotmail.com> wrote in message <br>
> news:iueq3l$hr2$1@newscl01ah.mathworks.com...<br>
> > I am trying to speed up part of my code but I cannot find a way to do it <br>
> > faster. This is the problem I am working on. I have 4 arrays: number and <br>
> > LKN_EN are each 67000*1. Hcat and LKN_ENcat are each 175000*11. I tried to <br>
> > combine them in the following way: My first try:<br>
> > tmp2=zeros(size(number,1),size(Hcat,2));<br>
> > for i=1:length(number)<br>
> > for j=1:size(Hcat,2) tmp(2i,j)=any(Hcat(:,j)==number(i)) && <br>
> > any(LKN_ENcat(:,j)==(LKN_EN(i)));<br>
> > end<br>
> > end<br>
> Let's take a step back. Please explain, in words rather than code or <br>
> equations, what you intend this code to do. ......<br>
> Steve Lord<br>
          <br>
I think Steve is right to ask what you wish this result to do for you. In the first place, tmp2 will have a whopping big 11,725,000,000 elements in it, and you are probably spending most of your compute time swapping virtual memory in and out. However, secondly it seems axiomatic that you will not be able to do anything useful with such an enormous array without further processing it to the point where a mere human being could make some sense out of it. The very name "tmp2" is a strong hint of that. This suggests that you might conceivably be able to get to such a final humanaccessible result without ever generating the entire tmp2 array. In other words we are (or at least I am) suggesting that you might be guilty of using a brute force technique on something that calls for far more finesse. <br>
<br>
Roger Stafford