From: Bluebird on
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
From: Jan Simon on
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
From: Bluebird on
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
From: Jan Simon on
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
From: Bluebird on
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
 |  Next  |  Last
Pages: 1 2 3
Prev: uniform grid on two points
Next: large matrices