# Thread Subject: Replacing for loops

 Subject: Replacing for loops From: Charles Dillon Date: 14 Jun, 2011 15:50:21 Message: 1 of 7 I have been attempting to write a programme to run through all possible combinations of elements from two lists and add the results of a calculation on each pair together. the following is my current code, which works: function ans = cor(train1, train2, tau) ans = 0; length1 = numel(train1); length2 = numel(train2); for n=1:length1     for m= 1:length2         ans = ans + exp(-abs(train1(n) - train2(m))/tau);     end end end tau is a scalar, while train1 and train2 are vectors of variable length. Can anyone give me a tip as to how to replace the for loops with something that will work faster? I'm quite new to MATLAB, so I apologise if it's something trivially easy that I've missed.
 Subject: Replacing for loops From: Corey Scheip Date: 14 Jun, 2011 16:10:18 Message: 2 of 7 "Charles Dillon" wrote in message ... > I have been attempting to write a programme to run through all possible combinations of elements from two lists and add the results of a calculation on each pair together. the following is my current code, which works: > > function ans = cor(train1, train2, tau) > ans = 0; > length1 = numel(train1); > length2 = numel(train2); > for n=1:length1 > for m= 1:length2 > ans = ans + exp(-abs(train1(n) - train2(m))/tau); > end > end > end > > tau is a scalar, while train1 and train2 are vectors of variable length. Can anyone give me a tip as to how to replace the for loops with something that will work faster? > > I'm quite new to MATLAB, so I apologise if it's something trivially easy that I've missed. I can't get rid of both loops, but can offer a suggestion to get rid of one of the loops: %%%%%%%%%%%%%%%% function ans = cor(train1, train2, tau) ans = 0;  for n=1:numel(train1) % length1 assignment unnecessary    ans = ans + sum(exp(-abs(train1(n) - train2)/tau));  end % for loop end % function loop %%%%%%%%%%%%%% ans + exp(-abs(train1(n) - train2)/tau); % this will give you a [1 x length(train2)] matrix, so I've added the "sum" command to add them all up and then add that value to the "ans" value. Help any?
 Subject: Replacing for loops From: Shawn Bonneau Date: 14 Jun, 2011 16:24:53 Message: 3 of 7 A bit off topic but you should avoid using the variable ans. It is already used by MATLAB for the results to calculations if they aren't assigned to a variable. For instance: 2+4; will yield ans=6 "Charles Dillon" wrote in message news:it7vvt\$es8\$1@newscl01ah.mathworks.com... > I have been attempting to write a programme to run through all possible > combinations of elements from two lists and add the results of a > calculation on each pair together. the following is my current code, which > works: > > function ans = cor(train1, train2, tau) > ans = 0; > length1 = numel(train1); > length2 = numel(train2); > for n=1:length1 > for m= 1:length2 > ans = ans + exp(-abs(train1(n) - train2(m))/tau); > end > end > end > > tau is a scalar, while train1 and train2 are vectors of variable length. > Can anyone give me a tip as to how to replace the for loops with something > that will work faster? > > I'm quite new to MATLAB, so I apologise if it's something trivially easy > that I've missed.
 Subject: Replacing for loops From: Roger Stafford Date: 14 Jun, 2011 17:21:04 Message: 4 of 7 "Charles Dillon" wrote in message ... > ans = 0; > length1 = numel(train1); > length2 = numel(train2); > for n=1:length1 > for m= 1:length2 > ans = ans + exp(-abs(train1(n) - train2(m))/tau); > end > end > end - - - - - - - - -   You can use the 'bsxfun' for this. I will assume that train1 and train2 are both column vectors or both row vectors.  a = sum(sum(exp(-abs(bsxfun(@minus,train1/tau,train2.'/tau))))):   As Shawn says, don't use 'ans' for the name of a variable. It is a reserved matlab name. Roger Stafford
 Subject: Replacing for loops From: Charles Dillon Date: 14 Jun, 2011 18:54:05 Message: 5 of 7 "Corey Scheip" wrote in message ... > "Charles Dillon" wrote in message ... > > I have been attempting to write a programme to run through all possible combinations of elements from two lists and add the results of a calculation on each pair together. the following is my current code, which works: > > > > function ans = cor(train1, train2, tau) > > ans = 0; > > length1 = numel(train1); > > length2 = numel(train2); > > for n=1:length1 > > for m= 1:length2 > > ans = ans + exp(-abs(train1(n) - train2(m))/tau); > > end > > end > > end > > > > tau is a scalar, while train1 and train2 are vectors of variable length. Can anyone give me a tip as to how to replace the for loops with something that will work faster? > > > > I'm quite new to MATLAB, so I apologise if it's something trivially easy that I've missed. > > > I can't get rid of both loops, but can offer a suggestion to get rid of one of the loops: > > %%%%%%%%%%%%%%%% > function ans = cor(train1, train2, tau) > ans = 0; > for n=1:numel(train1) % length1 assignment unnecessary > ans = ans + sum(exp(-abs(train1(n) - train2)/tau)); > end % for loop > > end % function loop > > %%%%%%%%%%%%%% > > ans + exp(-abs(train1(n) - train2)/tau); % this will give you a [1 x length(train2)] matrix, so I've added the "sum" command to add them all up and then add that value to the "ans" value. > > Help any? Thanks for the help! I ended up going with Roger's suggestion, but thank you for the contribution nonetheless. My reason for the length1 assignment was that I thought that calling the numel function on every iteration of the loop would take up time. A lecturer told me that it was good practice, though he was referring to C++ at the time. Thanks again!
 Subject: Replacing for loops From: Charles Dillon Date: 14 Jun, 2011 18:56:04 Message: 6 of 7 "Shawn Bonneau" wrote in message ... > A bit off topic but you should avoid using the variable ans. It is already > used by MATLAB for the results to calculations if they aren't assigned to a > variable. For instance: > > 2+4; > > will yield ans=6 > > > > "Charles Dillon" wrote in message > news:it7vvt\$es8\$1@newscl01ah.mathworks.com... > > I have been attempting to write a programme to run through all possible > > combinations of elements from two lists and add the results of a > > calculation on each pair together. the following is my current code, which > > works: > > > > function ans = cor(train1, train2, tau) > > ans = 0; > > length1 = numel(train1); > > length2 = numel(train2); > > for n=1:length1 > > for m= 1:length2 > > ans = ans + exp(-abs(train1(n) - train2(m))/tau); > > end > > end > > end > > > > tau is a scalar, while train1 and train2 are vectors of variable length. > > Can anyone give me a tip as to how to replace the for loops with something > > that will work faster? > > > > I'm quite new to MATLAB, so I apologise if it's something trivially easy > > that I've missed. Thanks for the advice. I hadn't even thought about the fact that I was overloading it. I'll bear that in mind.
 Subject: Replacing for loops From: Charles Dillon Date: 14 Jun, 2011 18:57:05 Message: 7 of 7 "Roger Stafford" wrote in message ... > "Charles Dillon" wrote in message ... > > ans = 0; > > length1 = numel(train1); > > length2 = numel(train2); > > for n=1:length1 > > for m= 1:length2 > > ans = ans + exp(-abs(train1(n) - train2(m))/tau); > > end > > end > > end > - - - - - - - - - > You can use the 'bsxfun' for this. I will assume that train1 and train2 are both column vectors or both row vectors. > > a = sum(sum(exp(-abs(bsxfun(@minus,train1/tau,train2.'/tau))))): > > As Shawn says, don't use 'ans' for the name of a variable. It is a reserved matlab name. > > Roger Stafford Thanks very much! That works perfectly! Much appreciated. I'll remember not to use ans in future.

## Tags for this Thread

### Add a New Tag:

Separated by commas
Ex.: root locus, bode

### 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.

Tag Activity for This Thread
Tag Applied By Date/Time
loops Charles Dillon 14 Jun, 2011 11:54:07
nested loops Charles Dillon 14 Jun, 2011 11:54:07
for loops Charles Dillon 14 Jun, 2011 11:54:07
Feed for this Thread