0

I have a case where I need 2 for loops (i and k) as shown below. And I want to continue with the inner loop after I left it.

import numpy as np
X = [[12, 11, 1], [1,2,3]]
mu = [1, 2, 3]
sublist = []

for i in range(0, 4):
    for k in range(0, 3):
        subtr = X[i] - mu[k]
        sublist.append(subtr)
    # leaving the loop k to calc argmin
    agmin = np.argmin(sublist)
    C.append(agmin)
        # Now I want to get back to the inner loop (k) to continue #further calculation, but obviously will result an error.
        np.dot((C[i] == k),X[i])    

What is the best way to deal with such cases?

6
  • The above code is not perfect and is just a example: It should rather be like: X = np.array([[12, 11, 1], [1,2,3]]), for k in range(0,2). But please note this is just to exhibit the inner and outer for loop calculation Commented Jun 16, 2017 at 23:02
  • Why do you need to leave the inner loop in the first place? Looks like the ordering doesn't need to be as it is. Or at least put the .dot operation inside the loop? Or even yet, start the inner loop over again! Commented Jun 16, 2017 at 23:02
  • > Why do you need to leave the inner loop in the first place? >>> Because I want t o do some calculation that is only for the outer loop and then use it back to inner loop. Commented Jun 16, 2017 at 23:05
  • What do you mean by ".dot" operation inside loop? How can you apply it here. Commented Jun 16, 2017 at 23:06
  • What do you mean by "is only for the outer loop"? I see no restriction in your code. Everything of the outer loop is available in the inner loop. You can do the calculation inside the inner loop. Are you trying to perform that operation only 4 times, instead of 12? If so, your logic is incorrect. Please post a MCVE. Commented Jun 16, 2017 at 23:28

1 Answer 1

2

do everything you need to do in the inner loop, before you leave the inner loop. Below is a slightly modified version of your code:

import numpy as np
X = [[12, 11, 1], [1,2,3]]
mu = [1, 2, 3]
sublist = []
C = #whatever C should be initialized to    

for i in range(0, 4):
    for k in range(0, 3):
        subtr = X[i] - mu[k]
        sublist.append(subtr)

        # calculate agmin (argmin) once per inner loop, at end
        if k == 2:
            agmin = np.argmin(sublist)
            C.append(agmin)

        # not sure what this line does, but do it inside the inner loop since it
        # needs k. (I'm guessing you really want some_var = np.dot(...) )
        np.dot((C[i] == k),X[i])            
Sign up to request clarification or add additional context in comments.

3 Comments

This is what the question is. If there is a case where one is not able to do all things in inner loop because it depends on the calculation done on the outer loop, what is solution.
The correct answer to that question is "show me the case, in code or pseudocode, because you almost certainly are approaching your problem the wrong way."
For the code you posted in your question, I've already given you a solution where all steps happen in the inner loop, including the key step you originally had in the outer loop. If you modify your question code in some way, I'll edit my answer to solve it for your new code. But the answer will still involve "do everything you need to do in the inner loop, before you leave the inner loop"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.