Message Boards Message Boards

0
|
4706 Views
|
14 Replies
|
8 Total Likes
View groups...
Share
Share this post:

How can I connect two Do[ ] loops?

Posted 3 years ago

Hi guys, I have the following problem, I have a "Do" loop, like

Do[ expression1; If[ "....."  ,PutAppend[{...},"list"]],{100}] 

In this case, it sees if the expression satisfies the condition inside the If, and if it does, it put it inside a list, if it does not, it just keeps going for another round, in this case, it does it 100 times. Now think that instead of that, when the loop find that a set of parameters satisfy the condition it goes to another "Do", like

 Do[ expresion2(depends on the result of the previous Do); If["......",PutAppend[{...},"list2"] ],{500}]

How can I do it in another way like,

Do[] (If it find a set of point satisfying the conditions move to another Do) -> Do[] ( here in this Do we put PutAppend and save the result if it satisfies the condition of this Do too) 

The structure of the Do is like

Do[ Expression, {number of iterations}] 

since there are two loops, we have two {number of iterations}, but my idea is just to have one.

Thank you.

POSTED BY: Felipe Villazon
14 Replies

I will try Hans. Here we go

Do[ m={{a1,a2},{a3,a4}}; (*2x2 matrix*)
a1 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}]; a3 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}];
a2 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}]; a4 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}];
If[a1>a2 && a3>a4,PutAppend[{a1,a2,a3,a4,m},"list"]],{100}]

At this point we stored the values for a1, a2, a3 ,a4 and m that satisfy the condition of the first Do loop. Now we are going to calculate the value for a matrix M, that is,

dn=ReadList["list"]; (*here I'm calling the list because I need the values found previously*)

Do[ M[i_]:= {{b1,b2},{b3.b4}}*dn[[i,3]]+dn[[i,5]]*b1 (*2x2 matrix where I'm using the data from l"ist"*)
b1 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}]; b3 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}];
b2 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}]; b4 = RandomReal[{0, 1.}]*RandomChoice[{-1., 1.}];
If[Eigenvalues[M[i]][[1]]> 0 && b1> dn[[i,1]] && 0<Eigenvectors[M[i]][[1,1]]<0.5,
PutAppend[{M[i],b1,b2,b3,b4,dn[[i,1]],dn[[i,2]],m},"list2"],{i,1,Length[dn]}]

In the second Do loop, I used the values from the first one, this is a simple example. But think about what if in the first Do loop I need to do 100000 iterations in order to get a lot of data and then use them in the second Do loop, It's not good for the memory of the code. How can I make that in every iteration that the first Do loop finds a set of values, go immediately to the second one to evaluate it?

In the case of a bigger matrix, with more conditions, and more restrictive constraints, what would be the better way to optimize it in time and memory?

POSTED BY: Felipe Villazon
Posted 3 years ago

Sorry Felipe, but this is not simple enough for me :)

And the second Do loop throws errors. Even after correcting some obvious typos/omissions.

POSTED BY: Hans Milton
Posted 3 years ago

But while your example is not so simple, it does show the general structure. So here is an alternative you could try to adapt:

fa[x_] := Divisible[x, 7] (* 1st condition *)
fb[x_] := Divisible[x, 3] (* 2nd condition *)

list = {};
Do[
    temp = RandomInteger[10000];
    If[fa[temp] && fb[temp], AppendTo[list, Sqrt@temp]],
    200
]
POSTED BY: Hans Milton
Posted 3 years ago

Felipe, an edit to my code. This should be closer to your example:

fa[x_] := Divisible[x, 7] (*1st condition*)
fb[x_] := Divisible[x, 3] (*2nd condition*)

list = {};
Do[
    temp1 = RandomInteger[10000];
    temp2 = RandomInteger[10000];
    If[
     fa[temp1] && fb[temp1 + temp2], AppendTo[list, Sqrt[temp1 + temp2]]
    ],
    200
]
POSTED BY: Hans Milton
Posted 3 years ago

From this and your earlier post I assume that you have a set of expressions in a file. And you want to take, from that file, those expressions which fulfills both of two conditions. Am I right?

If so it could be convenient to use the function Select:

file = Range@1000;
Select[file, Divisible[#, 3] && Divisible[#, 7] &]
POSTED BY: Hans Milton

Hans, from the first Do loop I store a set of values for those parameteres, those ones that satisfy the conditions. As you can see, I have no data in the list since I get the data from the fisrt Do loop. The first Do runs from 1 to n, where n can be any number depending on how many data I wanna collect. Once I have it, in the second Do loop I'm doing another calculation, in this case I'm calculating a particle mass, where the mass depends on the parameters that I found in the first Do loop, that's why the second Do run from 1 to Lenght(list), since I'm just evaluating the result found in the first Do to see if they satisfy the second Do loop, but think about the fact that I need to store a lot of data in the first Do to use it in the second one, it on't be good for the computer memory, that's why instead of acumulate data and then use it, I would like that those set of values to be used inmediatelly in the second Do, once it finish, we go back to the fisrt Do for another round.

POSTED BY: Felipe Villazon
Posted 3 years ago

Felipe, could you give a skeleton example of how you now do it with the two Do loops? Numerical and simple.

POSTED BY: Hans Milton
Posted 3 years ago

Filipe,

Maybe this will help a little, it basically does another Do loop if the number is odd.

listodd = {}; listeven = {}; Do[
 If[OddQ[i], AppendTo[listodd, i]; 
  Do[Print["This is and odd number", " -> ", i], {y, 1, 2}], 
  AppendTo[listeven, i]], {i, 1, 10}]; Print[listodd]; Print[listeven]

Regards.

POSTED BY: Paul Cleary

Paul, what would be the job of the variable "y" here?

POSTED BY: Felipe Villazon
Posted 3 years ago
Do[Print["This is and odd number", " -> ", i], {y, 1, 2}]

Is your second Do loop, the 'y' is just how many times you want it to loop, you can set that value from the first loop. You can use the values of {a1, a2, a3, a4 and m} in this loop. When it has finished clear all the values and go back to Loop 1. The amount of data you can handle is all down to how much memory you have in your pc. it's up to you to manage that.

I use Task manager to see how much memory is being used by my code. I have 32Gb, and many a time wish I had 64 and more.

POSTED BY: Paul Cleary

Filipe,

There are many ways to do something like this. Please provide a simple, working example. It’s difficult to help you without understanding exactly what you want.

Regards

POSTED BY: Neil Singer

Neil, I hope you are doing well. Here I posted the same question but with a code to understand in a better way what I really wany to my code to do and what it is doing actually. https://community.wolfram.com/groups/-/m/t/2283209?p_p_auth=tX7GmTVO

Sadly I have not had luck trying to solve it. I hope you can understand the idea.

POSTED BY: Felipe Villazon

Welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

Please do not duplicate discussions in your future posts. You can always edit your original discussion if changes are needed. Having the same question in two posts confuses other members and waste the solving efforts.

POSTED BY: Moderation Team

I won't, thanks

POSTED BY: Felipe Villazon
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract