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:
Getting rid of this for loop

Subject: Getting rid of this for loop

From: Bluebird

Date: 5 Mar, 2010 22:32:23

Message: 1 of 11

Hi Folks,

I have the following problem which i solve using for loop, but it is very slow that it is defeating the purpose of my whole implementation :( so your help is highly appreciated

I have a 4 vectors "Labels" of values 1 and -1 size mx1
                           "weights" has weights (random numbers)for each label element; hence size mx1
                            "error" has values in the range of [0:127]

therefore each element in the mx1 has label,weight and error

so what i want to do is for each value in the error, i want to find the sum of weights of labels ==1 and sum of weights of labels==-1

for a small exmple when labels= [-1 -1 1 1 1]
                                 weights=[0.1 0.4 0.2 0.2 0.1]
                                  error= [ 2 2 1 0 3], in case that error is [0:3]
so what i want is
error sum(weights(label==1 and error)) sum(weights(label==-1 and error))
0 0.2 0
1 0.2 0
2 0 0.1+0.4=0.5
3 0.1 0

the code that i am using is the following
FRange=[0:127]
for loop1=1:length(FRange)
    FRange(loop1,2)=sum(distr(check_error'==FRange(loop1,1) & TrainingLabels==-1));
    FRange(loop1,3)=sum(distr(check_error'==FRange(loop1,1) & TrainingLabels==1));
 end

but imagine when this 127 is instead of 2^7 is 2^10 or 2^15 then it will be very time consuming...

Looking forward for your help

Subject: Getting rid of this for loop

From: Jan Simon

Date: 6 Mar, 2010 00:12:23

Message: 2 of 11

Dear Bluebird!
 
> FRange=[0:127]
> for loop1=1:length(FRange)
> FRange(loop1,2)=sum(distr(check_error'==FRange(loop1,1) & TrainingLabels==-1));
> FRange(loop1,3)=sum(distr(check_error'==FRange(loop1,1) & TrainingLabels==1));
> end

I'm confused by the dimensions of FRange. At first it is 0:127 (btw: no need for additional square brackets!), then you access FRange(loop1, 2) and FRange(loop1, 3). I get the impression, that FRange grows in each loop, which is really a dramatical waste of time. Just pre-allocate FRange with its final size to be *much* faster.
Then check_error is transposed twice in each loop - why don't you do this once outsie the loop?
The comparisons "TrainingLabels==1" and "TrainingLabels==-1" can be move outside the loop also.

Good luck, Jan

Subject: Getting rid of this for loop

From: Bluebird

Date: 6 Mar, 2010 00:28:24

Message: 3 of 11

Thank you Jan,

I did preallocate the matrix... It didnt really make the loop very fast, it just got very slightly faster..

FRange is 127x3 :)

Is there anyway that i can get rid of the for loop such as using hist or such a think, i was actually trying to use hist but couldnt link it well to do what i want to do :(

Thank you everybody for your help

Subject: Getting rid of this for loop

From: Jan Simon

Date: 6 Mar, 2010 14:21:08

Message: 4 of 11

Dear Bluebird!

> Is there anyway that i can get rid of the for loop such as using hist or such a think, i was actually trying to use hist but couldnt link it well to do what i want to do :(

It would be easier to help, if you post a working example, e.g. with the definition of [distr].

Kind regards, Jan

Subject: Getting rid of this for loop

From: Bluebird

Date: 6 Mar, 2010 14:54:02

Message: 5 of 11

Thanks again Jan,

Here is a working example for a small set, in my case length of labels, weights and error goes to 15000 and n can be 10 so it really becomes very tediuos


n=2;
labels= [-1 -1 1 1 1 -1 -1];
weights=[0.1 0.15 0.2 0.2 0.1 0.05 0 2];
error= [2;2;1;0;3;1;3]; % error has values of 0:2^n-1

FRange=[0:2^n-1]'; % lets assume n=2 since the errors are between 0:3
for loop1=1:length(FRange)
    FRange(loop1,2)=sum(weights(error'==FRange(loop1,1) & labels==-1));
    FRange(loop1,3)=sum(weights(error'==FRange(loop1,1) & labels==1));
 end


Many many thank

Subject: Getting rid of this for loop

From: Jan Simon

Date: 6 Mar, 2010 16:42:08

Message: 6 of 11

Dear Bluebird!

> n=2;
> labels= [-1 -1 1 1 1 -1 -1];
> weights=[0.1 0.15 0.2 0.2 0.1 0.05 0 2];
> error= [2;2;1;0;3;1;3]; % error has values of 0:2^n-1
>
> FRange=[0:2^n-1]'; % lets assume n=2 since the errors are between 0:3
> for loop1=1:length(FRange)
> FRange(loop1,2)=sum(weights(error'==FRange(loop1,1) & labels==-1));
> FRange(loop1,3)=sum(weights(error'==FRange(loop1,1) & labels==1));
> end

Did you try my suggestion to avoid transposing [error] repeatedly and compare labels with 1 and -1 in every loop?
The repeated "& labels==-1" can be inserted in the variable "error" directly, e.g. (btw: do not use names of builtin functions for variables!):
  err2 = error';
  err2(labels ~= -1) = -1; % This does not appear in FRange
  err3 = error';
  err3(labels ~= 1) = -1;
  for loop1=1:length(FRange)
    FRange(loop1,2) = sum(weights(err2 == FRange(loop1,1)));
    FRange(loop1,3) = sum(weights(err3 == FRange(loop1,1)));
  end

This is the trivial method to accelerate loops by movin all possible calculation outside!
But anyhow, if FRange is a linear vector as 0:127, there is usually a method to use this to apply a direct indexing method and omit the loop completely.

Kind regards, Jan

Subject: Getting rid of this for loop

From: Bluebird

Date: 6 Mar, 2010 18:40:25

Message: 7 of 11

Thank you Jan :)

I will give it a shot and see. To be honest, i am not really sure how can i solve it by indexing. I was thinking if i can use some hist stuff which might help me avoid looping over all over , but i spend so much time trying to use it but could it link it well :( and achieve the same result.

Can you please help me with the indexing thing? I hope i can get rid of this loop. FRange is always linear and goes from 0:2^n-1

Thank you sir

Subject: Getting rid of this for loop

From: Bluebird

Date: 7 Mar, 2010 14:44:06

Message: 8 of 11

Any idea please ?

Subject: Getting rid of this for loop

From: us

Date: 7 Mar, 2010 14:51:02

Message: 9 of 11

"Bluebird " <wlouis@ryerson.ca> wrote in message <hn0e3l$ide$1@fred.mathworks.com>...
> Any idea please ?

did you try js's solution(?)...
if so, did it work(?)...
in case it didn't work: why did it not work(?)...
all questions that need to be answerer before you whine in this NG...

us

Subject: Getting rid of this for loop

From: Bluebird

Date: 8 Mar, 2010 15:31:19

Message: 10 of 11

Hello us and Jan, thanks for your help.

Sorry for getting back late, but yes i did try it and answered these questions earlier before i whine.

Jan solution did work, but still as 'n' in FRange ,where FRange is 0:2^n-1, increases the for loop becomes more and more time consuming..

For example when i tried it on a 250 items dataset using n=10; in my coding it took 1.2 sec, and in Jan's coding it took 0.8 sec... So it did reduce the time, but not significantly!

Jan was kind to give a hint about the index thing, but i couldnt figure out how to implement it ...

Thank you for your help

Subject: Getting rid of this for loop

From: Jan Simon

Date: 8 Mar, 2010 17:37:05

Message: 11 of 11

Dear Bluebird!

> Jan solution did work, but still as 'n' in FRange ,where FRange is 0:2^n-1, increases the for loop becomes more and more time consuming..
...
> Jan was kind to give a hint about the index thing, but i couldnt figure out how to implement it ...

It would be kind, if you post the code here. Then I do not have to program your solution again. Then I try further improvements.

Thanks, Jan

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