Prev: uniform grid on two points
Next: large matrices
From: Jan Simon on 6 Mar 2010 11:42 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
From: Bluebird on 6 Mar 2010 13:40 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
From: Bluebird on 7 Mar 2010 09:44 Any idea please ?
From: us on 7 Mar 2010 09:51 "Bluebird " <wlouis(a)ryerson.ca> wrote in message <hn0e3l$ide$1(a)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
From: Bluebird on 8 Mar 2010 10:31
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 |