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:
for loop into for loop.

Subject: for loop into for loop.

From: zak

Date: 20 May, 2011 09:50:05

Message: 1 of 7

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

Subject: for loop into for loop.

From: ImageAnalyst

Date: 20 May, 2011 11:53:04

Message: 2 of 7

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.

Subject: for loop into for loop.

From: Andy

Date: 20 May, 2011 12:18:04

Message: 3 of 7

"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?

Subject: for loop into for loop.

From: zak

Date: 20 May, 2011 14:49:04

Message: 4 of 7

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

Subject: for loop into for loop.

From: zak

Date: 20 May, 2011 14:57:05

Message: 5 of 7

Hello Andy,
Thanks for the answer.
This is a part of the code.

Subject: for loop into for loop.

From: ImageAnalyst

Date: 20 May, 2011 15:53:46

Message: 6 of 7

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.

Subject: for loop into for loop.

From: zak

Date: 20 May, 2011 16:50:05

Message: 7 of 7

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!!

Tags for this Thread

No tags are associated with 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