PDA

View Full Version : Loop within a loop within a loop... How to?


MikeRhone
03-19-2008, 06:45 PM
Ok... I can't seem to figure out a simple bit of MEL. What I need to do is loop through a set of XYZ coordinates. I'm know there has to be a nicer way to do this, I just cant seem to recall what it is.

The ouput I want is: 0 0 1, 0 0 2, 0 0 3, 0 1 1, 0 1 2, 0 1 3, 1 0 1, 1 0 2 etc... Any order is fine actually, as long as it loops through all 3 loops. Is this what is called a matrix?

Sorry for the super noobery.

int $xi, $yi, $zi;
for ($xi >=0; $xi <= 3; $xi++)
{
for ($yi >=0; $yi <= 3; $yi++)
{
for ($zi >=0; $zi <= 3; $zi++)
{
print ("This is coordinate " + $xi " + " + $yi " + " + $zi);
}
}
}

granthortin
03-19-2008, 07:26 PM
Try this:



int $xi, $yi, $zi;

for ($xi = 0; $xi <= 3; $xi++)
{

for ($yi = 0; $yi <= 3; $yi++)
{

for ($zi = 0; $zi <= 3; $zi++)
{
print ("This is coordinate " + $xi + " + " + $yi + " + " + $zi + "\n");
}

}

}

MikeRhone
03-19-2008, 07:30 PM
Ok, I got it working.. there were all sorts of syntax problems with that one, though I would be very interested to hear of a cleaner way to write that bit of code:

int $xi, $yi, $zi;
for ($xi =0; $xi <= 3; $xi++)
{
for ($yi =0; $yi <= 3; $yi++)
{
for ($zi =0; $zi <= 3; $zi++)
{
print ("This is coordinate " + $xi + $yi + $zi);
}
}
};

alredha
03-19-2008, 07:30 PM
what you have written is right, the three for loops are properly nested but :

// this is wrong, you've put two conditions
for($xi>=1,$i<=3,$i++)

//this is right, the first is initializing the var $xi, then the condition
for($xi=1,$i<=3,$i++)

and the concatination :

//this is wrong, string + variable | string + variable | string + variable
print ("This is coordinate " + $xi " + " + $yi " + " + $zi);

//this is right, string + variable + string + variable + string + variable
print ("This is coordinate " + $xi + " + " + $yi + " + " + $zi + "\n");

so the result is :

// you can leave the three variables declaration

int $xi, $yi, $zi;

for ($xi=0; $xi <= 3; $xi++)
{
for ($yi=0; $yi <= 3; $yi++)
{
for ($zi=0; $zi <= 3; $zi++)
{
print ("This is coordinate " + $xi + " + " + $yi + " + " + $zi + "\n");
}
}
}

// the \n at the end of the print is to return

i hope it helps.

GennadiyKorol
03-19-2008, 07:46 PM
What do you mean by a cleaner way?

Another way would be to use 1 loop and compute the indexes yourself:


int $size = 3;
int $n = $size*$size*$size;
int $m = $size*$size;
int $i;

for($i = 0; $i < $n; $i++) {
int $x = $i%$size;
int $y = ($i/$size)%$size;
int $z = $i/$m;

print (" " + $x + " " + $y + " " + $z + "\n");
}



Result:
0 0 0
1 0 0
2 0 0
0 1 0
1 1 0
2 1 0
0 2 0
1 2 0
2 2 0
0 0 1
1 0 1
2 0 1
0 1 1
1 1 1
2 1 1
0 2 1
1 2 1
2 2 1
0 0 2
1 0 2
2 0 2
0 1 2
1 1 2
2 1 2
0 2 2
1 2 2
2 2 2

MikeRhone
03-19-2008, 10:29 PM
You are a champion! Thank you so much. It took me a bit to figure out what your last code sample was actually doing (as the 3x3x3 grid was just a sample. I need to do it for various grid sizes). In any event, I got it going and learned something in the process... Thanks!

M

strarup
03-20-2008, 12:06 AM
Hi,

quite cool... however not sure it get's cleaner by using a Matrix thou...
took HenryKorol's code and throwed it on a matrix...


proc daHuhHuhTest()
{
matrix $daMatrix[4][3] = << 0, 0, 0; 1, 1, 1; 2, 2, 2; 3, 3, 3 >>;
//print($daMatrix[0][0]+" "+ $daMatrix[0][0]+" "+$daMatrix[0][0]+"\n");
int $size = 3;
int $m = $size*$size;
int $n = $size*$size*$size;
int $i;
for($i = 0; $i < $n; $i++)
{
int $x = $i/$m;
int $y = ($i/$size)%$size;
int $z = $i%$size;
print($daMatrix[$x][$z]+" "+ $daMatrix[$y][$z]+" "+$daMatrix[$z][$z]+"\n");
}
print("\n");
print($daMatrix);
}
daHuhHuhTest;


however it doesn't seem to get the "3" values...
tried to change int $n = $size*$size*$size+$m;
however then only one of the rows get to 3 and not the other 2... :)

kind regards

Strarup

GennadiyKorol
03-21-2008, 12:44 PM
Mike: No problemo :)

strarup:
I dunno, I'd personally go with nested loops for this kind of thing, but here's a bit modified version for the 3d grid of custom size:


// Define the size of the grid...
int $grid[] = {3, 2, 3};

int $s = $grid[0];
int $m = $grid[0]*$grid[1];
int $n = $grid[0]*$grid[1]*$grid[2];

for($i = 0; $i < $n; $i++) {
int $x = $i%$s;
int $y = ($i/$s)%$grid[1];
int $z = $i/$m;

print (" " + $x + " " + $y + " " + $z + "\n");
}


Notice that the middle column takes only 2 values now:

Result:
0 0 0
1 0 0
2 0 0
0 1 0
1 1 0
2 1 0
0 0 1
1 0 1
2 0 1
0 1 1
1 1 1
2 1 1
0 0 2
1 0 2
2 0 2
0 1 2
1 1 2
2 1 2

CGTalk Moderation
03-21-2008, 12:44 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.