Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Mixing and matching variables, speed up

Subject: Mixing and matching variables, speed up

From: Floris Zoutman

Date: 29 Jun, 2011 09:09:09

Message: 1 of 3

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:
My first try:
tmp2=zeros(size(number,1),size(Hcat,2));
for i=1:length(number)
    for j=1:size(Hcat,2)
        tmp(2i,j)=any(Hcat(:,j)==number(i)) && any(LKN_ENcat(:,j)==(LKN_EN(i)));
    end
end
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!
for i=1:length(number)
    tmp=Hcat==number(i) & LKN_ENcat==LKN_EN(i);
    tmp2(i,:)=sum(tmp);
end
Does anybody have a good suggestion to improve the code? Many thanks in advance

Subject: Mixing and matching variables, speed up

From: Steven_Lord

Date: 29 Jun, 2011 14:52:54

Message: 2 of 3



"Floris Zoutman" <fzoutman@hotmail.com> wrote in message
news:iueq3l$hr2$1@newscl01ah.mathworks.com...
> 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: My first try:
> tmp2=zeros(size(number,1),size(Hcat,2));
> for i=1:length(number)
> for j=1:size(Hcat,2) tmp(2i,j)=any(Hcat(:,j)==number(i)) &&
> any(LKN_ENcat(:,j)==(LKN_EN(i)));
> end
> end
> 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!
> for i=1:length(number)
> tmp=Hcat==number(i) & LKN_ENcat==LKN_EN(i);
> tmp2(i,:)=sum(tmp);
> end
> Does anybody have a good suggestion to improve the code? Many thanks in
> advance

Let's take a step back. Please explain, in words rather than code or
equations, what you intend this code to do. I _think_ if I understand your
code correctly ISMEMBER with the 'rows' flag will help you, but I'm not
certain.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Mixing and matching variables, speed up

From: Roger Stafford

Date: 30 Jun, 2011 00:29:09

Message: 3 of 3

"Steven_Lord" <slord@mathworks.com> wrote in message <iufe85$fkg$1@newscl01ah.mathworks.com>...
> "Floris Zoutman" <fzoutman@hotmail.com> wrote in message
> news:iueq3l$hr2$1@newscl01ah.mathworks.com...
> > 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: My first try:
> > tmp2=zeros(size(number,1),size(Hcat,2));
> > for i=1:length(number)
> > for j=1:size(Hcat,2) tmp(2i,j)=any(Hcat(:,j)==number(i)) &&
> > any(LKN_ENcat(:,j)==(LKN_EN(i)));
> > end
> > end
> Let's take a step back. Please explain, in words rather than code or
> equations, what you intend this code to do. ......
> Steve Lord
- - - - - - - - - - -
  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 human-accessible 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.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us