f



for loop into for loop.

Hello this is my first time that i am writing here.. 
I need your help to improve my code. This is my code

for a=1:numberOfShortestPaths
    for b=a+1:a+numberOfShortestPaths
        for c=b+1:b+numberOfShortestPaths
            for d=c+1:c+numberOfShortestPaths
                for e=d+1:d+numberOfShortestPaths
                    total_weight=weight(:,:,a)+weight(:,:,b)+weight(:,:,c)+weight(:,:,d)+weight(:,:,e);
                    maximum=max(total_weight);
                end
            end
         end
    end
end

i want to do this function "automatically" because the letters are depending from other parameter(number of calls). in this example the number of calls=5.
 if the number=4 the for loops would be until 'd'.. i dont know how to write the code to do this automatic and not manually like that one   
Do you have any ideas to recommend me?
Thanks in advance
Zack
0
5/20/2011 9:50:05 AM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

6 Replies
2190 Views

Similar Articles

[PageSpeed] 27

Zack:
I think you need to look into recursive programming.

The function would call itself.  You'd have to pass along the
information as to when the function should "bail out", in other words,
backtrack out or recurse out.

0
imageanalyst (7621)
5/20/2011 11:53:04 AM
"zak " <zaxos_pele@hotmail.com> wrote in message <ir5dgd$12q$1@newscl01ah.mathworks.com>...
> Hello this is my first time that i am writing here.. 
> I need your help to improve my code. This is my code
> 
> for a=1:numberOfShortestPaths
>     for b=a+1:a+numberOfShortestPaths
>         for c=b+1:b+numberOfShortestPaths
>             for d=c+1:c+numberOfShortestPaths
>                 for e=d+1:d+numberOfShortestPaths
>                     total_weight=weight(:,:,a)+weight(:,:,b)+weight(:,:,c)+weight(:,:,d)+weight(:,:,e);
>                     maximum=max(total_weight);
>                 end
>             end
>          end
>     end
> end
> 
> i want to do this function "automatically" because the letters are depending from other parameter(number of calls). in this example the number of calls=5.
>  if the number=4 the for loops would be until 'd'.. i dont know how to write the code to do this automatic and not manually like that one   
> Do you have any ideas to recommend me?
> Thanks in advance
> Zack

You are overwriting total_weight and maximum inside the loop, therefore losing all of the information except for the final iteration of the loop.  I'm guessing this is unintended?
0
5/20/2011 12:18:04 PM
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a982d13b-9ab7-4b80-8b6e-18b61a3dcadb@24g2000yqk.googlegroups.com>...
> Zack:
> I think you need to look into recursive programming.
> 
> The function would call itself.  You'd have to pass along the
> information as to when the function should "bail out", in other words,
> backtrack out or recurse out.

Hi,thanks for the reply
i know that the solution will come recursively but i am stack in this part, i dont know how to write it.
I would be thankful if you could help me more.. 
0
5/20/2011 2:49:04 PM
Hello Andy,
Thanks for the answer.
This is a part of the code.
0
5/20/2011 2:57:05 PM
On May 20, 10:57=A0am, "zak " <zaxos_p...@hotmail.com> wrote:
> Hello Andy,
> Thanks for the answer.
> This is a part of the code.

---------------------------------------
We know it's a part of the code, but we were wondering if you really
meant
total_weight=3Dtotal_weight+weight(:,:,a)+weight(:,:,b)+weight(:,:,c)
+weight(:,:,d)+weight(:,:,e);
instead of
total_weight=3Dweight(:,:,a)+weight(:,:,b)+weight(:,:,c)+weight(:,:,d)
+weight(:,:,e);

And you should study up on recursive programming.  I can't really give
you a tutorial on it in a newsgroup reply.
0
imageanalyst (7621)
5/20/2011 3:53:46 PM
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7c722e19-5209-4378-8cb0-cad1d947803f@w21g2000yqm.googlegroups.com>...
> On May 20, 10:57 am, "zak " <zaxos_p...@hotmail.com> wrote:
> > Hello Andy,
> > Thanks for the answer.
> > This is a part of the code.
> 
> ---------------------------------------
> We know it's a part of the code, but we were wondering if you really
> meant
> total_weight=total_weight+weight(:,:,a)+weight(:,:,b)+weight(:,:,c)
> +weight(:,:,d)+weight(:,:,e);
> instead of
> total_weight=weight(:,:,a)+weight(:,:,b)+weight(:,:,c)+weight(:,:,d)
> +weight(:,:,e);
> 
> And you should study up on recursive programming.  I can't really give
> you a tutorial on it in a newsgroup reply.

Yes this is what I meant :
total_weight=total_weight+weight(:,:,a)+weight(:,:,b)+weight(:,:,c)
 +weight(:,:,d)+weight(:,:,e);

Thanks for the replies anyway!!
0
5/20/2011 4:50:05 PM
Reply:

Similar Artilces:

For loop while loop
I need to evaluate a function from 0:200 then create an array of every 5th value in the 0:200 array using a for loop then using a while loop. I can't get the for loop to work at all because I cannot use >,< controllers, only an =. So I focused on the While loop: n=0:200;%creates 201 cell array from 0-200 x=n.^2+.25;%calculates x plotcol=(length(n)-1)/5;%=(201-1)/5=40 y=zeros(1,plotcol);%creates 1X40 vector col=1;%used for incrementing in while loop while col<=plotcol; y(1,col)=x(1,5*col); col=col+1; end Unfortunately the output starts at 16 when it should start ...

Loop in a loop?
Hi, I'm new to Python and have come across a problem I don't know how to solve, enter com.lang.python :) I'm writing some small apps to learn the language, and I like it a lot so far. My problem I've stumbled upon is that I don't know how to do what I want. I want to do a loop in a loop. I think. I've got two arrays with some random stuff in, like this. array1 = ['one','two','three','four'] array2 = ['a','b','c','d'] I want to loop through array1 and add elements from array2 at the end, so it looks lik...

loops in loops
In the attached vi, the one second loop runs at one second and can't be touched.&nbsp; In the ten second loop, the ten second clock is a stand in for a process that generates a finished signal. The problem is once the process is complete, to start it again, leave the one second loop running, and start an additional process that will take less time than 10 seconds. thanks internal loops.vi: http://forums.ni.com/attachments/ni/170/319512/1/internal loops.vi Hi exo, what do you try there? If you start your vi, the value from your stop button will be read and after that, both loops &quo...

while loop in a while loop
Hi all, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I am trying to write a program with a while loop within another while loop. When I run the vi both while loops&nbsp; run but then only the inner while loop seems to update, I adjust controls and they have no effect on the indicators. &nbsp; Thanks.. You need to familiarize with the concept of dataflow programming. Run your VI in "execution highligthing" mode while watching the diagram and you'll get a better feeling on how things work. :) &nbsp; If both loops need to run in para...

LOOP and +LOOP
With LOOP, since the index (I) increases by 1 with each loop iteration, we can test for equality as a means to determine if it's time to exit the loop or not: : TEST 10 0 DO I . LOOP ; TEST 0 1 2 3 4 5 6 7 8 9 Here, when I gets to 10, the loop exits. It's not so easy with +LOOP, since the index can be changed by any arbitrary value on each iteration: : TEST 100 0 DO I . RANDOM +LOOP ; Here, we cannot test for equality. We have to test for crossing a boundary instead, at least as far as I can see. With that in mind, are these following results correct: : TEST 10 0 DO I . 1 +LOOP ...

FOR loop not looping
I have the below script that uses 2 for loops. for some reason they loop is not looping ! /bin/ksh CURR_PATH='/main/nedcor/flexcube' cd $CURR_PATH for DIR1 in 'WIP' 'INPUT';do echo $DIR1 for DIR in $(find . -name $DIR1);do RESULT=`ls -A $DIR | egrep -v "BKUP|BKP"` echo $DIR echo $RESULT echo $RESULT1 if [ -z "$RESULT" ] ; then echo "No Files found" exit 0 else echo file $RESULT found exit 1 ...

loop not looping
This should be relatively easy for someone more experienced to pick out what I'm doing wrong here...All I want is my loop to run as many times as there are .dat files in the specified directory. I've hacked up my code a bit just so i can get it to work and realize it's not as simple or efficient as it could be. My script only runs through the loop once and I can't figure out why it never loads 'binary_002.dat' and so on. thanks for your help counter_value = 1; %counter value set back to 0 when while loop = false slice_thicknessMM = 1.5; tf = isdir('C:\Documen...

For loop over a while loop
Hi, I am computing a quantity that depends on two variables. The computation itself is iterative (i'm using 3 while loops to compute it). Now i want to do a sensitivity analysis of the input variables and do a 3D plot where I can start making sense out of my results, however not sure how to write a for loop over these while loops to compute different combinations of what I define as my parametric space for my two input variables. Thanks in advance, Vash Please add a code example and/or more specific question.... ...

while loop in a while loop
Hi All, I have the following situation: while(rs.next()) { //loop1 while(rs2.next()) { //loop 2 } } It seems like loop 2 is only beeing used once. Should I after loop2 put the cursor back to the beginning? if so, how do I do that? Thanks! -- Posted by news://news.nb.nu Steven wrote: > Hi All, > > I have the following situation: > > > while(rs.next()) { //loop1 > > while(rs2.next()) { > //loop 2 > } > > } > The architypal loop (for a List eg ArrayList of Blah objects) is: for(It...

WHILE LOOP AS FOR LOOP
its possible? For each id in (SELECT * FROM table) INSERT INTO table2(id,fk,nombre) values(1,id,'xxx'); Jessica Gonz�lez (jess.rgm@gmail.com) writes: > its possible? > > For each id in (SELECT * FROM table) > INSERT INTO table2(id,fk,nombre) > values(1,id,'xxx'); > So in SQL, you don't normally code the loops explicitly - the loops are hidden on the inside. Logically, it is an operation of a set. INSERT table(id, fk, nombre) SELECT 1, id, 'xxx' FROM table -- Erland Sommars...

Having trouble stopping a loop (or loop de loop de loop)
Hi again everyone, Here is my rather clunky code (and I know it is rather inefficient): ------------------------------------------------------------------------------------------------------------------------------ while e_t lt 2 do begin for x=4e003, 20e003, 4e003 do begin for c=0.006, 0.01, 0.004 do begin for o=(50*!pi/180), (70*!pi/180), (10*!pi/180) do begin v= (a*x) ind_small = where(file[1,*] lt 2*x,count) if count eq 0 then te_small=0 if count gt 0 then begin ext_small = file[*,ind_small] le_small= ext_small[1,*] te_small = total(le_small^3) endif ind_large = where(file[1,*] ge...

what diference between timed loop and while loop and for loop
If I want to count 2 second working in the loop and next then going out of the loop I don't know&nbsp;which the loop &nbsp;I can use Message Edited by Jairak on 07-26-2008 03:14 PM Why do you need a loop to count for 2 seconds? To learn more about LabVIEW it is recommended that you go through the tutorial(s) and look over the material in the NI Developer Zone's <a href="http://zone.ni.com/devzone/cda/tut/p/id/5054" target="_blank">Learning Center</a> which provides links to other materials and other tutorials. You can also take the <a href=...

Nested FOR loops. (a loop within a loop)
I can't get my head around them. I have two MySQL tables, one contains the categories, the other contains the posts. Each post is linked to a category. Now I'm trying to display each post with a drop down menu that has the categories in it and have the category that the post is in selected. Here's my code for the drop down menu where my woes lie. for ($i=0; $i<count($posts); $i++) { for ($j=1; $j<=count($categories); $j++) { if ($j == $post[$i]['id']) { selected = ' selected="selected"'; } else { sele...

Triple nested loop python (While loop insde of for loop inside of while loop)
try to make my triple nested loop working. My code would be: c = 4 y1 = [] m1 = [] std1 = [] while c <24: c = c + 1 a = [] f.seek(0,0) for columns in ( raw.strip().split() for raw in f ): a.append(columns[c]) x = np.array(a, float) not_nan = np.logical_not(np.isnan(x)) indices = np.arange(len(x)) interp = interp1d(indices[not_nan], x[not_nan], kind = 'nearest') p = interp(indices) N = len(p) dt ...

Web resources about - for loop into for loop. - comp.soft-sys.matlab

iPhone, iPad, Mac, Apple opinion and news - The Loop
The Loop provides comprehensive and insightful news, editorial, and commentary on iPhone, iPod, Macintosh, associated third-party software and ...

Eschborn-Frankfurt City Loop - Wikipedia, the free encyclopedia
The Eschborn-Frankfurt City Loop (until 2008 Rund um den Henninger-Turm , sometimes called the Frankfurt Grand Prix in English; in 2010 Rund ...

WA youth research to close loop on diabetes with trial at Perth’s Princess Margaret Hospital
WA youngsters and adults with type 1 diabetes are helping researchers develop a more advanced artificial pancreas system that promises to reduce ...

Water pouring into leaking City Loop
A video covertly filmed by a train driver appears to show water pouring into the City Loop near Parliament station.

Jeremy’s 5: Apple and Accessories at CES, Siri Remote Loop, Akinator + F2P Games
... second, the annual event’s seemingly declining interest to Apple fans. I also wanted to share some additional thoughts on Apple’s Remote Loop ...

Review: Apple Watch replica Milanese Loop band from JETech
Company: JETech Product: Milanese Loop Band Compatibility: 38mm or 42mm Apple Watch Price: $21.99 (42mm) or $22.99 (38mm) Website: http://i ...

YouTube Loop
Back in 2014 , I found a way to loop YouTube videos using a feature of the HTML5 player. It looks like YouTube added "loop" as a standard feature. ...

Loop is a Digital Photo Frame That Looks Like a ’50s Television
... wrapping it in a kitschy exterior is one of the surest ways to our hearts. Photo sharing is pretty well-worn ground at this point, but Loop ...

Get ready for official Space Black Milanese Loop Apple Watch bands
Apple appears to be set to launch a brand new Apple Watch band in the form of a 42mm Space Black Milanese Loop band for Space Black steel watches ...

Here Is A "Loop Video" Of Ken Griffey Jr.'s Perfect Swing For The Hall Of Fame Voter Who Wanted One
MLB.com baseball writer Marty Noble has turned in his Baseball Hall of Fame ballot , and from the deep pool of eligible candidates, he has selected ...

Resources last updated: 2/3/2016 12:06:17 PM